473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Casting a string to an enum

kc
I'm trying to pull data from a database that I have no control over the
structure of. There's a members table with a column for the member's
sex. It just stores the sex as M or F. I'd like to create an enum to
store the sex.

Is there a way that I can create a method in the Enum to convert M to
Male and F to Female, or does this method have to exist in another
class?

Thanks for any help!

Nov 21 '05 #1
8 8586


kc wrote:
I'm trying to pull data from a database that I have no control over the
structure of. There's a members table with a column for the member's
sex. It just stores the sex as M or F. I'd like to create an enum to
store the sex.

Is there a way that I can create a method in the Enum to convert M to
Male and F to Female, or does this method have to exist in another
class?


There is. Although it can only convert strings into enum values with
*exactly* matching names. The syntax is a bit fiddly:

'somewhere, define the enum
Public Enum Sex
F
M
End Enum

'in a procedure:
Dim sFromDatabase As String = "M" 'for example
Dim sx As Sex
Try
sx = CType([Enum].Parse(GetType(Sex), sFromDatabase), Sex)
Catch argex As ArgumentException
'sFromDatabase was neither 'M' nor 'F'
End Try

The fiddly syntax is because Parse, being inherited from System.Enum,
returns an Object, which we must convert to a Sex (or whatever). The [
] round Enum in Enum.Parse is because Enum is a VB keyword, so we must
escape it with [ ] in order to refer to the System.Enum class.

--
Larry Lard
Replies to group please

Nov 21 '05 #2
Hi,

Enum.Parse() can parse a string to a enum member, but the string should
exactly match the enum member. If your case, if you have the enum defined
as:

Enum Sex
M
F
End Enum

then [Enum].Parse (GetType (Sex), "M") will get you Enum.M (after casting).

HTH.

"kc" <je******@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I'm trying to pull data from a database that I have no control over the
structure of. There's a members table with a column for the member's
sex. It just stores the sex as M or F. I'd like to create an enum to
store the sex.

Is there a way that I can create a method in the Enum to convert M to
Male and F to Female, or does this method have to exist in another
class?

Thanks for any help!
Nov 21 '05 #3
kc
And I'm cool with that...... Except I'd really prefer:

Public Enum Sex
Male
Female
End Enum

rather than using M and F. I have this issue with abbreviations. ;-) So
can I get the enum to do the conversion from M to Male?

(At the moment, I'm resigned to having my Data Mapper doing it for me)

Nov 21 '05 #4
Nope.

"kc" <je******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
And I'm cool with that...... Except I'd really prefer:

Public Enum Sex
Male
Female
End Enum

rather than using M and F. I have this issue with abbreviations. ;-) So
can I get the enum to do the conversion from M to Male?

(At the moment, I'm resigned to having my Data Mapper doing it for me)
Nov 21 '05 #5
"kc" <je******@gmail.com> schrieb:
And I'm cool with that...... Except I'd really prefer:

Public Enum Sex
Male
Female
End Enum

rather than using M and F. I have this issue with abbreviations. ;-) So
can I get the enum to do the conversion from M to Male?


\\\
Public Enum SexDatabase
M = 0
F = 1
End Enum

Public Enum Sex
Male = SexDatabase.M
Female = SexDatabase.F
End Enum
..
..
..
Dim s As SexDatabase = SexDatabase.F
Dim Sex As Sex = CType(s, Sex)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #6
On 22 Jul 2005 09:41:56 -0700, kc wrote:
And I'm cool with that...... Except I'd really prefer:

Public Enum Sex
Male
Female
End Enum

rather than using M and F. I have this issue with abbreviations. ;-) So
can I get the enum to do the conversion from M to Male?

(At the moment, I'm resigned to having my Data Mapper doing it for me)


Add a second Enum.

Public Enum Sex
Male = 1
Female = 2
End Enum

Public Enum Sex2
M = 1
F = 2
End Enum

Dim sFromDatabase As String = "M" 'for example
Dim sx As Sex
Try
sx = CType([Enum].Parse(GetType(Sex2), sFromDatabase), Sex)
Catch argex As ArgumentException
'sFromDatabase was neither 'M' nor 'F'
End Try
Nov 21 '05 #7
Errata:
Public Enum SexDatabase
M = 0


It's better to use a value <> 0...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #8
Hi Herfried ! :O)
It's better to use a value <> 0...


Could explain that a little bit more ?

I may see two reasons why this is true, i just want to know if you have any
others and/or the same..

1. Any Enum type values has a default value of 0. That's why I usually set
the default member of the Enum with a value of 0 or create a "NotSet"
member.

2. I didn't test that much, but i've noticed that when you connect to a
WebService that exposes a Enum type (with FlagsAttribute), the values
getting map to client (in the reference.vb file) does not always follow the
values on the server..

I mean if you have the following Enum exposed by the WS :
'***
<Flags()> _
Enum MyEnum
Val1 = 0
Val2 = 1
Val3 = 2
Val4 = 4
End Enum
'***

You get this on the client side :
'***
<Flags()> _
Enum MyEnum
Val1 = 1
Val2 = 2
Val3 = 4
Val4 = 8
End Enum
'***

Thanks for your input.

--
Best Regards
Yanick
Nov 21 '05 #9

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

Similar topics

3
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought...
2
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
1
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum...
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
6
by: Jason Larion | last post by:
When working with enums, I've noticed some behaviour that seems completely counter-intuitive to me. I was wondering if someone here could help restore my sanity, or at least help me to understand...
3
by: Vincent Finn | last post by:
Hi, I am trying to write a generic function and it isn't behaving as expected. I want to avoid having to write custom convert functions for my enums so I want to convert to an int and cast to...
2
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
4
by: mitdej | last post by:
Hi there, I have an several enum types that starts from a nunmber other than 0. For example: public enum InternalStatus { Pending = 1, Ported = 2, Suspended = 3 } I put this values in a int...
4
by: Rene | last post by:
If variance/covariance is not allowed in array of value types, why is the expression on the "Main" method run successfully (see snippet below)? I am missing something? Thank you. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.