Wednesday, March 7, 2012

How to use database values in an enum or class, so developer has intellisense support....

I have a SQL database table with all languages used in my application.

I would like to use an enum or constant class with all the languages, so every developer of the app sees directly what languages are available.

Can this be done?

Use Enum.Parse method as shows following code snippet:

enum Language{ English, German, Slovak}Language lang = (Language)Enum.Parse(typeof(Language),"Slovak");
|||

That will not work, I think I have to work the way around.

Put all constants in a constant class and then insert these values into a SQL database table.

Anyone a better solution?

|||Why not work? When you put all enum values into database and after (when you will read values from database) you use Enum.Parse(typeof(Language), VALUE_FROM_DATABASE) method it will work.|||So with the parse method, you can add values to the enum?|||

Maybe you can just put your languages into drop down list ( you have to display them anyway to the user to see or select ) and just use this control as storage for you languages info? It will work mostly as enum you have language name and its ID (index) if you need. You can do a lot with it.

Thanks

JPazgier

No comments:

Post a Comment