0

I'm trying to find a way to add colors to a "palette". The worst part of not learning computer science is that it is hard to word questions. I've been searching for adding enumerations in a class during run time. Because ideally, I can do a Console.ReadLine() to add a new color.

class Program
{
    static void Main(string[] args)
    {
        Color _Color = new Color();

        Console.WriteLine(Palette.Black);
        // _Color.addColor(Console.ReadLine(), Console.ReadLine());
        Console.ReadKey();
    }
}

public class Palette
{
    private readonly string color;
    public static readonly Palette Black = new Palette("black");
    public static readonly Palette Red = new Palette("red");
    public static readonly Palette Green = new Palette("green");
    public static readonly Palette Blue = new Palette("blue");

    //I want to be able to create new colors by using the Color.addColor() method

    private Palette(string inputColor) { this.color = inputColor; }
    public override string ToString() { return this.color; }
}

public class Color
{
    public void addColor(string colorName, string color) 
    {
        //create a new color into Palette using form:
        // public static readonly Palette colorName = new Palette("color");
    }
}

I want to be able to do Console.WriteLine(Palette.Yellow) after inputting into the Console that I want to add the color yellow into the palette.

km00000
  • 31
  • 2
  • 1
    You can't. But why do you want to do that as a field anyway? How would you expect to *use* something that was only added while you were executing the code? Sounds like you probably want a `Dictionary` of colours... – Jon Skeet Jun 10 '16 at 20:42
  • I will build a DLL and would use the DLL to add or go through the palette of colors. But I'll try a Dictionary. – km00000 Jun 10 '16 at 20:45
  • By the time your code is running, the DLL has already been built though... – Jon Skeet Jun 10 '16 at 20:46
  • True. I basically wanted to make a database of colors that I can add to – km00000 Jun 10 '16 at 21:11
  • So you want to persist that? Store it in a data file (XML, JSON, whatever) - or in an actual database. – Jon Skeet Jun 10 '16 at 21:18
  • I'll try out a dictionary first. If that doesn't work out like I want, I go to using an actual database. Thanks! – km00000 Jun 10 '16 at 21:23
  • Well if you just create a `Dictionary<,>`, that isn't going to stick around between runs. This is the problem with trying to help when you haven't clearly described what you're trying to achieve... – Jon Skeet Jun 10 '16 at 21:26

2 Answers2

0

I think you should start with maybe redesigning your application to use a collection of colors instead of fields on palette. However you might be able to achieve what you're looking for with ExpandoObject. I found it from a different article here.

Community
  • 1
  • 1
skalpin
  • 310
  • 1
  • 10
  • A collection as in a list? I was using a list initially, but I could not do Palette.____ where Visual Studio would list out all the possible values. – km00000 Jun 10 '16 at 21:18
0

C# is not designed in a way for you to dynamically create variables. Like @skalpin said, a redesign in pretty much the only solution for you.

carefulnow1
  • 803
  • 12
  • 30