Re: how to parse an Enum Structure in vb.net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



It seems a waste to use strings as opposed to integers in a database for
ENUMS. You can just as easily assign numbers to each enum value, i.e.,

Enum Month
January = 10
February = 20
March = 30
. . .

In the new enum;
Enum Month
January = 10
February =20
Filibuster = 25
March =30
. . .

--
Dennis in Houston


"Phill W." wrote:

giannik wrote:
I have an Enum

Public Enum MyEnum
EnumVal1=0
EnumVal2=1
EnumVal2=2
end enum

I save in an access database this enum value as an integer (0=EnumVal1,
1=EnumVal2, 2=EnumVal)

Save the Enum /names/ not their numeric values.
OK, you only have three here, but what if you had a long list of these
and then, Lord forbid, you added another one "in the middle"? Much of
your existing data (in Access) would be wrong.

Use a String column in the database and use <value>.ToString() to get
the enum "name" to save.

When retreving this enum from the database how do I ensure that the correct
value is passed to my object

When reading the property back, parse the string value (from Access)
back into the Enum Type, as in

MyObject.MyEnum = CType( dr.Item( "EV" ), MyEnum )

By way of a rather silly example:

Class MyClass
Enum Month
January = 1
February
March
. . .

dr.Item( "Month" ) = Month.December.ToString() ' actually "December"

Now, just for the sake of argument, let's create a new month, called
Filibuster, between February and March.

Class MyClass
Enum Month
January = 1
February
Filibuster
March
. . .

Oh No! I hear you cry. You'll have to bulk update all the records in
Access to increment their month numbers!
Nope. Holding the Enum /names/ means you don't have to. Assuming you
already have a row in there for December:

? dr.Item( "Month" ).GetType().ToString()
[System.]String
? dr.Item( "Month" ).ToString()
"December"
? CType( dr.Item( "Month" ), Month ).GetType().ToString()
[MyClass.]Month
? CType( dr.Item( "Month" ), Month )
December

So far, so good, but here's the clincher ...

? CInt( dr.Item( "Month" ), Month )
13

.... even if it was 12 when you saved that record into Access!

HTH,
Phill W.

.



Relevant Pages

  • RE: Get a type from a string
    ... name of an enum value stored in a database and want to get back the enum ... > database so that I can set my property values dynamically. ... > string. ... > Imports System ...
    (microsoft.public.dotnet.framework.windowsforms)
  • using intellisense to pull data from a database
    ... often made mistakes by typing the wrong string in. ... appropriately by quering the database for the corresponding string value. ... add it to the enum list and recompile. ... Basically I just want the developer to be ...
    (microsoft.public.dotnet.general)
  • Re: Load Data from Database as Class Properties or enum
    ... Right now I have created a duplicate enum class representing my Database ... >> One more thing is if I use reflection than I guess I won't be able to use ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Reflection - integer values directly to enum
    ... The SetValue using the value from the database was returning a casting ... Besides not wanting to convert an int to a string to an enum for performance ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reversing java to c enum style mapping
    ... Mark Space wrote: ... MySQL for example will expand database values to words for ... enum-like support. ... Last time I checked I think the enum words appeared as part of the table schema, and the actual values you retrieved were just ints, unless you made a new table and did a join, manually. ...
    (comp.lang.java.programmer)