Dynamic Enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Wright

    #1

    Dynamic Enum

    I have an enum based on a lookup table in my database. However, when a new
    item is added to the lookup table or one is removed, I have to modify the
    enum to match. Is there a way to dynamically load the enum when the class
    is instantiated? I was thinking a stored procedure to load it but can't
    figure out how to do this.

    John


  • Samuel R. Neff

    #2
    Re: Dynamic Enum


    Enums are compile-time constants so you can't change them at runtime
    based on data. I would instead suggest using a pre-build script to
    regenerate the enum based on data or using a code-gen tool.

    If the table values can change at runtime, then it's probably not
    appropriate to be using an enum in the first place.

    HTH,

    Sam

    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.



    On Mon, 17 Sep 2007 15:33:52 -0600, "John Wright"
    <riley_wrightx@ hotmail.comwrot e:
    >I have an enum based on a lookup table in my database. However, when a new
    >item is added to the lookup table or one is removed, I have to modify the
    >enum to match. Is there a way to dynamically load the enum when the class
    >is instantiated? I was thinking a stored procedure to load it but can't
    >figure out how to do this.
    >
    >John
    >

    Comment

    • Bob Powell [MVP]

      #3
      Re: Dynamic Enum

      Rather than modifying the enum, which is a compiled thing, you should be
      populating your lookup table directly from the database using a request.

      I would use a datareader to get all the items in the table and then put them
      in a list of strings then bind the list to the column / dropdon , grid or
      whatever it is you're using to display the list.

      --
      --
      Bob Powell [MVP]
      Visual C#, System.Drawing

      Ramuseco Limited .NET consulting


      Find great Windows Forms articles in Windows Forms Tips and Tricks


      Answer those GDI+ questions with the GDI+ FAQ


      All new articles provide code in C# and VB.NET.
      Subscribe to the RSS feeds provided and never miss a new article.


      "John Wright" <riley_wrightx@ hotmail.comwrot e in message
      news:u1g1yJX%23 HHA.1168@TK2MSF TNGP02.phx.gbl. ..
      >I have an enum based on a lookup table in my database. However, when a new
      >item is added to the lookup table or one is removed, I have to modify the
      >enum to match. Is there a way to dynamically load the enum when the class
      >is instantiated? I was thinking a stored procedure to load it but can't
      >figure out how to do this.
      >
      John
      >

      Comment

      • John Wright

        #4
        Re: Dynamic Enum

        This is the route I am going to take. I was thinking an enum would be great
        based on table definitions. Not that they would change all that much, but
        the do change and I wanted to make sure my enum would capture that. Since I
        can't do this, I am going to just access the table directly. Thanks for the
        help.

        John
        "Bob Powell [MVP]" <bob@spamkiller bobpowell.netwr ote in message
        news:3550EF78-B241-480A-BE1E-E7D6B3FC301C@mi crosoft.com...
        Rather than modifying the enum, which is a compiled thing, you should be
        populating your lookup table directly from the database using a request.
        >
        I would use a datareader to get all the items in the table and then put
        them in a list of strings then bind the list to the column / dropdon ,
        grid or whatever it is you're using to display the list.
        >
        --
        --
        Bob Powell [MVP]
        Visual C#, System.Drawing
        >
        Ramuseco Limited .NET consulting

        >
        Find great Windows Forms articles in Windows Forms Tips and Tricks

        >
        Answer those GDI+ questions with the GDI+ FAQ

        >
        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.
        >
        >
        "John Wright" <riley_wrightx@ hotmail.comwrot e in message
        news:u1g1yJX%23 HHA.1168@TK2MSF TNGP02.phx.gbl. ..
        >>I have an enum based on a lookup table in my database. However, when a
        >>new item is added to the lookup table or one is removed, I have to modify
        >>the enum to match. Is there a way to dynamically load the enum when the
        >>class is instantiated? I was thinking a stored procedure to load it but
        >>can't figure out how to do this.
        >>
        >John
        >>
        >

        Comment

        Working...