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. The Lounge
  3. Reconciling Enums and Lookup Tables

Reconciling Enums and Lookup Tables

Scheduled Pinned Locked Moved The Lounge
question
20 Posts 15 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.
  • B Brady Kelly

    We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

    E Offline
    E Offline
    Ennis Ray Lynch Jr
    wrote on last edited by
    #8

    I like using classes instead of enums these days.

    Need a C# Consultant? I'm available.
    Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

    1 Reply Last reply
    0
    • D Douglas Troy

      I was thinking more like Janet Jackson Nasty. :rolleyes:


      :..::. Douglas H. Troy ::..
      Bad Astronomy |VCF|wxWidgets|WTL

      E Offline
      E Offline
      El Corazon
      wrote on last edited by
      #9

      Douglas Troy wrote:

      I was thinking more like Janet Jackson Nasty.

      hmmmm a flashing enum? I have never heard of one....

      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

      D 1 Reply Last reply
      0
      • E El Corazon

        Douglas Troy wrote:

        I was thinking more like Janet Jackson Nasty.

        hmmmm a flashing enum? I have never heard of one....

        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

        D Offline
        D Offline
        Dan Neely
        wrote on last edited by
        #10

        El Corazon wrote:

        hmmmm a flashing enum? I have never heard of one....

        here you go. enum TrafficLightTypes { 1: Steady; 2: Flashing; }

        Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

        D 1 Reply Last reply
        0
        • B Brady Kelly

          We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #11

          In our case, we use a string representation of the enum outside the application (persistence in files, across communications, and so on). The numeric value doesn't matter in this case, and can change. There are some border cases you have to handle (when an enumeration value is removed), but it works fairly well. One thing the Ada programming language had going for it was all of this happened automatically with enumerated types.

          Software Zen: delete this;
          Fold With Us![^]

          1 Reply Last reply
          0
          • B Brady Kelly

            We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

            M Offline
            M Offline
            Miszou
            wrote on last edited by
            #12

            I use a helper class that I call ValueMap with static functions for retrieving data. It's sole purpose is to match strings to values by doing a lookup in a specialized database table. This way, I can use a string to describe the value I need, and let the ValueMap class translate it into a number for me. For example, in our application we deal a lot with two specific countries: USA and Canada. All the other countries are important, but aren't used as nearly much. So, if I need to know the database ID for Canada, I don't want to hardcode it into the application, but I still need to be able to reference it programatically. (For initializing combo boxes with a default country for example). What I have done is create a second table that contains mappings of strings to commonly used database ID's and default values.

            Property

            Value

            Country.CountryID.USA

            75

            Country.countryID.Canada

            32

            So, to find the database ID for Canada, I would do something like this: int canadaID = ValueMap.GetValueInt32( "Country.CountryID.Canada" ); Your ValueMap class can retrieve the values from the relevant storage (SQL server, XML files etc) and even cache them for faster subsequent retrieval. My own personal naming convention is [Table].[Column].[Value] as you can see in the example, but you can use whatever strings you like to identify the values. I'm sure there are probably better ways of doing this, but it's working fine for me. I hope it helps :)

            Sunrise Wallpaper Project | The StartPage Randomizer | The Windows Cheerleader

            1 Reply Last reply
            0
            • B Brady Kelly

              We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

              R Offline
              R Offline
              Robert Surtees
              wrote on last edited by
              #13

              Google comes up with Enums powered by Reflection[^] from some obscure developer site. :)

              1 Reply Last reply
              0
              • D Dan Neely

                El Corazon wrote:

                hmmmm a flashing enum? I have never heard of one....

                here you go. enum TrafficLightTypes { 1: Steady; 2: Flashing; }

                Otherwise [Microsoft is] toast in the long term no matter how much money they've got. They would be already if the Linux community didn't have it's head so firmly up it's own command line buffer that it looks like taking 15 years to find the desktop. -- Matthew Faithfull

                D Offline
                D Offline
                Douglas Troy
                wrote on last edited by
                #14

                more like ... enum FlashingType { TrenchCoat, BathRobe, OpenBlouse, WardrobeMalfunction } :rolleyes:


                :..::. Douglas H. Troy ::..
                Bad Astronomy |VCF|wxWidgets|WTL

                P 1 Reply Last reply
                0
                • B Brady Kelly

                  We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

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

                  For one small application I have I chose to use a pair of static readonly dictionaries (<int,string> and <string,int>) which I fill from the table when the app starts up.

                  1 Reply Last reply
                  0
                  • B Brady Kelly

                    We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

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

                    Perhaps the table should be updated at runtime to reflect the contents of the enum rather than the other way around. That would have saved me some trouble yesterday... when somebody emptied one of my translation tables. :mad:

                    B 1 Reply Last reply
                    0
                    • D Douglas Troy

                      more like ... enum FlashingType { TrenchCoat, BathRobe, OpenBlouse, WardrobeMalfunction } :rolleyes:


                      :..::. Douglas H. Troy ::..
                      Bad Astronomy |VCF|wxWidgets|WTL

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

                      No Flags attribute?

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Perhaps the table should be updated at runtime to reflect the contents of the enum rather than the other way around. That would have saved me some trouble yesterday... when somebody emptied one of my translation tables. :mad:

                        B Offline
                        B Offline
                        Brady Kelly
                        wrote on last edited by
                        #18

                        PIEBALDconsult wrote:

                        Perhaps the table should be updated at runtime to reflect the contents of the enum rather than the other way around.

                        I really like that idea, I'll just have to tighten up DB security to prevent direct inserts.

                        1 Reply Last reply
                        0
                        • B Brady Kelly

                          We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

                          L Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #19

                          I (and my previous manager) have been thru this, although it sounds like a good idea, it is not, unless its a static lookup table that will probably never change. Given that you are possibly stubborn and lazy, you could write a prebuild script to regenerate enums from the tables before you compile. Should not be hard (and should be a fun exercise).

                          xacc.ide - now with IronScheme support
                          IronScheme - 1.0 alpha 2 out now

                          1 Reply Last reply
                          0
                          • B Brady Kelly

                            We all love to use enums instead of magic numbers, but what happens when the enums are based on a lookup table than can change? It seems like there's room here from some interesting dynamic code, possibly building the enum dynamically, but the conflict is with actual coding using the enum, as code can actually only use values present when the code is written, unless there is some even more outlandish way to use runt-time enum values.

                            S Offline
                            S Offline
                            Syed Muhammad Fahad
                            wrote on last edited by
                            #20

                            Hmmmmm So you want some one to do this enum SomeEnum{ FirstElement=1, SecondElement=2 } Enum.ReAssign(typeof(SomeEnum),SomeEnum.FirstElement,3); :laugh: I don't think there is any microsoft guy around here who can introduce this in .Net 4.0 :doh:

                            Syed Muhammad Fahad Application Development Tyler Technologies -- TEMS Division mfahad@mazikusa.com

                            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