473,796 Members | 2,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to parse an Enum Structure in vb.net

I have an Enum Structure

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)
When retreving this enum from the database how do I ensure that the correct
value is passed to my object

For instance
MyObject.MyEnum =1 .
Does this populate the property with MyObject.MyEnum = EnumVal2
giannik
Jul 5 '06 #1
6 3003
"giannik" <gi*****@newsgr oups.nospamschr ieb:
>I have an Enum Structure

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)
\\\
Dim e As MyEnum = CType(v, MyEnum)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jul 5 '06 #2
You have nothing special to do. See EnumVal2 has being just a symbolic name
for the value 1 (similarly to a constant).

So if MyObject.MyEnum =1, MyObject.MyEnum =MyEnum.EnumVal 2 because
MyEnum.EnumVal2 is also 1.

--
Patrice

"giannik" <gi*****@newsgr oups.nospama écrit dans le message de news:
%2************* *****@TK2MSFTNG P05.phx.gbl...
>I have an Enum Structure

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)
When retreving this enum from the database how do I ensure that the
correct value is passed to my object

For instance
MyObject.MyEnum =1 .
Does this populate the property with MyObject.MyEnum = EnumVal2
giannik

Jul 5 '06 #3
ok. Thank you very much

"Patrice" <sc****@chez.co mwrote in message
news:e8******** ******@TK2MSFTN GP05.phx.gbl...
You have nothing special to do. See EnumVal2 has being just a symbolic
name for the value 1 (similarly to a constant).

So if MyObject.MyEnum =1, MyObject.MyEnum =MyEnum.EnumVal 2 because
MyEnum.EnumVal2 is also 1.

--
Patrice

"giannik" <gi*****@newsgr oups.nospama écrit dans le message de news:
%2************* *****@TK2MSFTNG P05.phx.gbl...
>>I have an Enum Structure

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)
When retreving this enum from the database how do I ensure that the
correct value is passed to my object

For instance
MyObject.MyEnu m=1 .
Does this populate the property with MyObject.MyEnum = EnumVal2
giannik


Jul 5 '06 #4
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>.ToStrin g() 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().ToS tring()
[System.]String
? dr.Item( "Month" ).ToString()
"December"
? CType( dr.Item( "Month" ), Month ).GetType().ToS tring()
[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.
Jul 5 '06 #5
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>.ToStrin g() 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().ToS tring()
[System.]String
? dr.Item( "Month" ).ToString()
"December"
? CType( dr.Item( "Month" ), Month ).GetType().ToS tring()
[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.
Jul 5 '06 #6
Storing the integer value rather than the name does perform better.
Storing the names is something that should be considered in some cases,
though.

In some cases the names correspond naturally to a number that won't
change, for an example the months that are numbered from 1 through 12.
Then there is no reason to store the name instead of the number.

In other cases the names have no natural numbering at all, then it might
be better to store the names. The numbers are only used internally by
the application, and the data in the database is meningful without a
translation table.

Dennis wrote:
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
. . .
Jul 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
369
by: ME | last post by:
What are the .NET Compact Framework Alternatives to Enum.Parse and Enum.Format as they are not supported by the Compact Framework? Thanks, Matt
5
1487
by: tony collier | last post by:
I have ..... enum day {monday, tuesday, wednesday}; myArray=5; i=(int)Enum.Parse(typeof(day), wednesday);
4
5739
by: marc.gibian | last post by:
I have been trying to improve the quality of my C# and ADO.NET coding. One of the books I've read strongly advises against using string values to address individual values in DataRow objects. This rings true to me after years of avoiding string lookups whenever possible. But, when I attempted to implement this recommendation I appear to run into casting issues. Thus: enum myFields { field1 = 0, field2 = 1
8
6343
by: cadilhac | last post by:
Hi, I have the following code: public enum MyColors { Red, Green, Blue } MyColors c = (MyColors)Enum.Parse(typeof(MyColors), "abcd"); Why do I get c = "abcd" after this code instead of getting a ArgumentException ? MSDN says that this expetion is triggered when value is a name, but not
4
1818
by: imme929 | last post by:
I got things working until I tried adding this enum to a structure... Public Enum Keyboard EnglishUS EnglishUK Spanish German Italian French
10
2250
by: Michael Feld | last post by:
Hello, does anyone know how to produce a data type which offers Enum-like IntelliSense in VS 2005? What I am trying to create is a type which is very similar to an enum, i.e. has a fixed set of values, but provides some more methods. Unfortunately, you cannot derive from System.Enum to do this. I noticed that if you type "Dim C as System.Drawing.Color = ", the editor will display a list of colors, but this are actually ReadOnly Shared
1
3304
by: Joe HM | last post by:
Hello - I have two Enums for which I would like to define type conversions ... Public Enum eA A2 = 0 A2 = 1 End Enum Public Enum eB B1 = 2
3
4030
by: John A Grandy | last post by:
Is this the hardest method in the entire .NET class lib? Seems like it works differently for different enumTypes I code SearchRequest.SafeSearch = (SafeSearchOptions)Enum.Parse(SafeSearchOptions,"moderate",true); and it tells me that "SafeSearchOptions is a type but is used like a
2
3565
by: shapper | last post by:
Hello, I have the following class ( Level is just a simple enumeration ): public class Theme { public Subject Subject { get; set; } public List<LevelLevels { get; set; } public string Note { get; set; } }
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.