By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Casting a string to an enum

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.