Casting a string to an enum

  • Thread starter Thread starter kc
  • Start date Start date
K

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!
 
kc said:
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.
 
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.

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!
 
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)
 
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
 
Nope.

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)
 
kc said:
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)
///
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top