enum on database values

Posted on 2005-03-03
Medium Priority
Last Modified: 2012-08-13

Can anybody show me hints to creating a dynamic database based enumeration?

Lets say I have a Country class and this class can used with ie. "Country.Germany" or "Country.Taiwan" - each representing the PK values from the Country table in DB. How do I make a class that automatically takes new rows from the Country table in DB into the enum?

Thanx in advance.
Question by:korona5
  • 2

Expert Comment

ID: 13450327
Hi, korona5.

What do you mean by "automatically takes new rows from the Country table in DB into the enum" ?
Why do you want to use your enum if its properties are created dinamically?
Do you want to generate C# code for the enum?


Author Comment

ID: 13456763
Lets say I have a Country class today - based on database values from a Country table - and I use it on my GUI like:

Country _Country = new Country(Country.Countries.Germany);

This way I don't have to do:

Country _Country = new Country(738);

"738" tells me nothing when reading my code, so I have to look up the database to see that 738 = Germany, or assume that programmers will write comments to the code and not change database values.

I can handwrite this enum each time I add a new Country to the database, but isn't it possible to make the code dynamically reflect changes in the database, so when I one day wish to add the Country "Congo", the class automatically can be instantiated with:

Country _Country = new Country(Country.Countries.Congo);

Accepted Solution

gena17 earned 500 total points
ID: 13469668

I just want to be sure I get you in correct way.
Say, your DB doesn't contain Congo.
I've compiled your application and have an executable.
Now you add Congo to the DB.
To use:
  Country _Country = new Country(Country.Countries.Congo);
you must go to your application, write new code and re-compile it.
In this case you need to add the enum member Congo manually.
Or you can write a small application that reads the DB and creates C# class code with enum members you want. After that you need to re-compile.

If you don't want re-compile you can use a class that determines countries by string, not enum.

Hope this helps.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question