How to create ENUM using values from the database C# RRS feed

  • Question

  • User166505692 posted

    I have a status table in database whose values I am using across the application. Status Table will have (ID, NAME). I want to create a StatusEnum which I can use in my code in the application. How can I create ENUM using values from database ?

    Currently I have enum like this

     enum StatusCode: int
            Open = 20,
            Received = 21,
            Delivered= 22,
            Cancelled = 23

    But I want to set the values from database.

    Tuesday, July 22, 2014 2:35 PM

All replies

  • User465171450 posted

    I don't think you can really do that, and what would be the point? If you create an enum dynamically from a database, what happens to your code when your enum gets changed? What happens if StatusCode.Cancelled no longer exists in the database? Your code will suddenly fail since the enumeration you are looking for and have hard-coded isn't in the enum anymore. 

    Tuesday, July 22, 2014 3:49 PM
  • User166505692 posted

    You are right that is why I don;t want to hardcode them I want them dynamically taken from DB. Question is if value changes in DB how can my application get updated value ?

    Tuesday, July 22, 2014 3:58 PM
  • User-328728277 posted

    if you get the value from a DB you don't need to create an enum, just get the value from a DB and if you change the value int the database it will change in your application too.

    Tuesday, July 22, 2014 5:18 PM