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
}
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.
enumcC#.net
Don't forget to mark useful responses as Answer if they helped you towards a solution.
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 ?
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.
Member
24 Points
178 Posts
How to create ENUM using values from the database C#
Jul 22, 2014 02:35 PM|sbmdude|LINK
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 c C#.net
All-Star
26071 Points
5892 Posts
Re: How to create ENUM using values from the database C#
Jul 22, 2014 03:49 PM|markfitzme|LINK
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.
enum c C#.net
Member
24 Points
178 Posts
Re: How to create ENUM using values from the database C#
Jul 22, 2014 03:58 PM|sbmdude|LINK
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 ?
enum c C#.net
Member
172 Points
67 Posts
Re: How to create ENUM using values from the database C#
Jul 22, 2014 05:18 PM|Dan Palacios|LINK
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.
enum c C#.net