Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Constant table?

Constant table?

Scheduled Pinned Locked Moved C#
questionperformancehelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Y_R
    wrote on last edited by
    #1

    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.

    C P 2 Replies Last reply
    0
    • Y 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.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      Y 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        Y Offline
        Y Offline
        Y_R
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Y 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.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          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.

          Y 1 Reply Last reply
          0
          • P PIEBALDconsult

            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.

            Y Offline
            Y Offline
            Y_R
            wrote on last edited by
            #5

            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.

            P 1 Reply Last reply
            0
            • Y Y_R

              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.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Microsoft tends to do the hard stuff and leave the easy stuff to you. (Which is better than the other way around.)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups