469,282 Members | 1,907 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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
Sep 17 '07 #1
3 6051

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"
<ri***********@hotmail.comwrote:
>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
Sep 18 '07 #2
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
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"John Wright" <ri***********@hotmail.comwrote in message
news:u1****************@TK2MSFTNGP02.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
Sep 18 '07 #3
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]" <bo*@spamkillerbobpowell.netwrote in message
news:35**********************************@microsof t.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
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
"John Wright" <ri***********@hotmail.comwrote in message
news:u1****************@TK2MSFTNGP02.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

Sep 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bob Shafer | last post: by
6 posts views Thread by Materialised | last post: by
8 posts views Thread by Eyeawanda Pondicherry | last post: by
2 posts views Thread by Chowdary Doppalapudi | last post: by
15 posts views Thread by rwf_20 | last post: by
4 posts views Thread by mark.olszowka | last post: by
9 posts views Thread by Mike Hofer | last post: by
4 posts views Thread by Steve Teeples | last post: by
6 posts views Thread by =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post: by
21 posts views Thread by arnuld | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.