TechTalkz.com Logo

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Dynamic Enum

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 18-09-2007, 04:36 AM   #1
John Wright
Guest
 
Posts: n/a
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


  Reply With Quote
Old 18-09-2007, 09:33 AM   #2
Samuel R. Neff
Guest
 
Posts: n/a
Re: Dynamic Enum


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"
<> wrote:

>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
>


  Reply With Quote
Old 18-09-2007, 04:32 PM   #3
Bob Powell [MVP]
Guest
 
Posts: n/a
Re: Dynamic Enum

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


Find great Windows Forms articles in Windows Forms Tips and Tricks


Answer those GDI+ questions with the GDI+ FAQ


All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.


"John Wright" <> wrote in message
news:.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
>


  Reply With Quote
Old 22-09-2007, 02:43 AM   #4
John Wright
Guest
 
Posts: n/a
Re: Dynamic Enum

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]" <> wrote in message
news:...
> 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
>
>
> Find great Windows Forms articles in Windows Forms Tips and Tricks
>
>
> Answer those GDI+ questions with the GDI+ FAQ
>
>
> All new articles provide code in C# and VB.NET.
> Subscribe to the RSS feeds provided and never miss a new article.
>
>
> "John Wright" <> wrote in message
> news:.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
>>

>



  Reply With Quote
Old 07-02-2009, 07:44 AM   #5
Newbie
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0 Rickoshay is an unknown quantity at this point


OS: Windows XP Windows Server 2003 / Windows Server 2008 Linux


Re: Dynamic Enum

Quote:
Originally Posted by John Wright View Post
I have an enum based on a lookup table in my database.
On a related note, I stopped using pure lookup tables long ago. Remove the lookups and replace with a string column. The join is gone while queries and reports still make sense. Tell your ORM (Entity Framework, LINQ to SQL, NHibernate) the column is an enum and referential integrity is achieved without a join. Indicate the ORM should save the string value versus the integer value, and magic numbers vanish.

Now, if you load up your "lookups" with all manner of ancillary columns, you really have a joinable table there, not a lookup per se.
Rickoshay is offline   Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataType Enum? Johnny J. VB.NET 3 17-09-2007 06:31 PM
DataType Enum? Johnny J. C#(C Sharp) 3 17-09-2007 06:31 PM
Re: Enum, switch, and a null Wojtek Java 5 11-09-2007 08:32 AM
converting string in enum !!! Harald VC.NET 2 05-09-2007 03:54 PM
enum base class Jure Bogataj C#(C Sharp) 5 05-09-2007 03:48 PM


< Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 02:34 AM.


vBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2009, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional