Home All Groups Group Topic Archive Search About

enum item from string

Author
13 Apr 2005 8:16 PM
Zorpiedoman
I want to set a property, that is an enum type, but I only have the string
available:

Enum EggTypes
    OverEasy
    Scrambled
    Poached
End Enum

Public myCookStyle as EggTypes

Public Sub OrderEggs(Request as String)
    'Here is where I want to set the value of
    'myCookStyle based on the Request String
    '... and NO I cannot change this code...
    '(This code just demonstrates what I am doing.. I ONLY have the string
    'or at BEST I have an integer that corosponds to the Integer value
    'of the EggType)

    myCookStyle = ????
End Sub


Thanks in Advance...

--
--Zorpie

Author
13 Apr 2005 8:26 PM
Tom Shelton
In article <3F4A32CB-CCFF-4605-833E-0FAC63788***@microsoft.com>, Zorpiedoman wrote:
Show quoteHide quote
> I want to set a property, that is an enum type, but I only have the string
> available:
>
> Enum EggTypes
>     OverEasy
>     Scrambled
>     Poached
> End Enum
>
> Public myCookStyle as EggTypes
>
> Public Sub OrderEggs(Request as String)
>     'Here is where I want to set the value of
>     'myCookStyle based on the Request String
>     '... and NO I cannot change this code...
>     '(This code just demonstrates what I am doing.. I ONLY have the string
>     'or at BEST I have an integer that corosponds to the Integer value
>     'of the EggType)
>
>     myCookStyle = ????
> End Sub
>
>
> Thanks in Advance...
>

For the string value, you might want to look at Enum.Parse

myCookStyle = _
    CType ([Enum].Parse (GetType (EggTypes), _
    Request), EggTypes)

This will of course throw and exception if the value is not defined in
the enum.  You can use the Enum.GetNames if you would like to do a
comparison ahead of time :)

--
Tom Shelton [MVP]
Author
13 Apr 2005 9:56 PM
Zorpiedoman
Perfect!  Thanks!
Author
13 Apr 2005 8:51 PM
Peter Lillevold
Hey Zorpiedoman,

you can use the Enum.Parse method to convert a string to the corresponding
enum value like this:

Dim myCookStyle As EggTypes = CType([Enum].Parse(GetType(EggTypes), Request),
EggTypes)

Regards,
Peter Lillevold
http://blogs.eternia.cc/peter

Show quoteHide quote
> I want to set a property, that is an enum type, but I only have the
> string available:
>
> Enum EggTypes
> OverEasy
> Scrambled
> Poached
> End Enum
> Public myCookStyle as EggTypes
>
> Public Sub OrderEggs(Request as String)
> 'Here is where I want to set the value of
> 'myCookStyle based on the Request String
> '... and NO I cannot change this code...
> '(This code just demonstrates what I am doing.. I ONLY have the
> string
> 'or at BEST I have an integer that corosponds to the Integer value
> 'of the EggType)
> myCookStyle = ????
> End Sub
> Thanks in Advance...
>
Author
13 Apr 2005 9:25 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"Zorpiedoman" <nowherem***@beatles.com> schrieb:
>I want to set a property, that is an enum type, but I only have the string
> available:
>
> Enum EggTypes
>    OverEasy
>    Scrambled
>    Poached
> End Enum
>
> Public myCookStyle as EggTypes
>
> Public Sub OrderEggs(Request as String)
>    'Here is where I want to set the value of
>    'myCookStyle based on the Request String
>    '... and NO I cannot change this code...
>    '(This code just demonstrates what I am doing.. I ONLY have the string
>    'or at BEST I have an integer that corosponds to the Integer value
>    'of the EggType)
>
>    myCookStyle = ????
> End Sub

\\\
myCookStyle = _
    DirectCast( _
        [Enum].Parse(GetType(EggTypes), Request), _
        EggTypes _
    )
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>