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