Greetings, Mostly I use enumerations to specify a list of constants that are connected. However, recently I needed to create a code that does the following: Paint a line with a specific color that is set by the value of the bits in a variable. The bits values are arranged in enumeration: enum bits { Switch1 = 1, Switch2 = 2, Switch3 = 4, switch4 = 8, } What is the best way to assign a color to each switch? I thought of two ways: --------------------------------------------- creating a dictionary and set its values in runtime: Dictionary Bits_Colors = new Dictionary; Bits_Colors.Add(switch1, Color.Red); Bits_Colors.Add(switch2, Color.Ivory); ..
But this solution because the dictionary is not constant and readonly would not help here. ------------------------------------------------- Creating another enumeration for the colors and using thier names as params to Color. enum BitsColors { Red = 1, Ivory = 2. Blue = 4, Green = 8 } string name = Enum.GetNames(bits, (int) switch2 ); // name = Ivory. Color c = Color.MakeByName(name);
----------------------------------------------------------- I wrote the code from my memory so it might be a little not working.
Sincerely yours Y.R.