Creating ENUMs & a Database..

stephenmbell

Member
Joined
Nov 25, 2008
Messages
5
Programming Experience
1-3
This may be a very simple question that, for some reason, I am not wrapping my head around... but - if someone could explain this it would be fantastic.

I don't have much experience with object oriented development other than back in college - but my question is regarding creating ENUMS.

The way I understand enums is that they are "user-defined types" - quite possibly a real world example would be an enum called status. Status could have possible values:


1 - Setup
2 - Pending
3 - Accepted

I am pretty sure that I understand the VB code to create this enum - but my question is - is it typical to hard code this into your vb file?? Wouldn't it make more sense to have a database table Status with the values you want for the enum??

IF the database table is the way it is done -- how do you do this?


Thanks for any responses, hope this post makes sense.

sb
 
is it typical to hard code this into your vb file?
Not only is it typical, it's the ONLY way to do it. Enumerations are types. You can't just create enumerations at run time based on data you read from a database. It's too late to create types then.

For values that are not really going to change, like a status, you simply hard-code an enumeration that matches the database, or even don't use a database table at all. If a change does need to be made then you update the database and the VB code at the same time.

For values that may change regularly you simply wouldn't use an enumeration. You'd have to validate against the database in any property setters that were to accept only those values.
 
Wouldn't it make more sense to have a database table Status with the values you want for the enum??

Why?

Suppose we are designing a kettle. The kettle's status is either going to be:

Boiling
NotBoiling


Hence at design time we have thought of all the statuses we want to code for and write code for them. A kettle cannot be anything else than boiling or not boiling water.

Now suppose we could do your database idea, and some clever kid comes along and adds a status HalfWayToBoiling
Your app contains no code for this status, and it doesnt really have any purpose or meaning - what do you hope it will do?
 
Back
Top