Constant table?
-
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.
-
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.
How about an array ? A constant array of colors, and you reference it by index, using the value that is the combination of your bit values.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
How about an array ? A constant array of colors, and you reference it by index, using the value that is the combination of your bit values.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Thanks for your reply. I thought of that but there are two problems: First, I don't think there is an easy way to make array a contant (except for basic types arrays). Second, my bits values are not 1, 2, 3, 4... (which would fit to an array). They are 1, 2, 4, 8, ... (Which requires some logic to be implemented in an array). I thought of constant dictionary but I don't think there is a way to make the dictionary constant or readonly.
Sincerely yours Y.R.
-
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.
You could use attributes, but the code can be verbose and (perhaps) inefficient. I'd stick with the
readonly static Dictionary
idea; wrap it in something so the code that uses it can't change it. -
You could use attributes, but the code can be verbose and (perhaps) inefficient. I'd stick with the
readonly static Dictionary
idea; wrap it in something so the code that uses it can't change it. -
I thought that this is the only way. It strange that such a powerful language don't have this simple features. Thanks for your help.
Sincerely yours Y.R.
Microsoft tends to do the hard stuff and leave the easy stuff to you. (Which is better than the other way around.)