Home All Groups Group Topic Archive Search About

Convert string to enum

Author
19 Apr 2006 6:26 PM
Tim Johnson
I know I can do this:

string value = myenum.enum1.ToString();  //= "myenum1"

but how can I do the reverse?  Given a string "myenum1", how do I turn it
into myenum.enum1?  I was hoping there's some more clever way than this:

myenum x = value=="myenum1" ? myenum.enum : ...;

Author
19 Apr 2006 6:36 PM
Kevin Spencer
Check out the following article. It's really not too hard to do:

http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

Show quote
"Tim Johnson" <TimJohn***@discussions.microsoft.com> wrote in message
news:F79095F0-6E80-4B79-9228-30915DEDC998@microsoft.com...
>I know I can do this:
>
> string value = myenum.enum1.ToString();  //= "myenum1"
>
> but how can I do the reverse?  Given a string "myenum1", how do I turn it
> into myenum.enum1?  I was hoping there's some more clever way than this:
>
> myenum x = value=="myenum1" ? myenum.enum : ...;
Author
19 Apr 2006 6:38 PM
Jon Skeet [C# MVP]
Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> I know I can do this:
>
> string value = myenum.enum1.ToString();  //= "myenum1"
>
> but how can I do the reverse?  Given a string "myenum1", how do I turn it
> into myenum.enum1?  I was hoping there's some more clever way than this:
>
> myenum x = value=="myenum1" ? myenum.enum : ...;

MyEnum foo = Enum.Parse (typeof (MyEnum), value);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
19 Apr 2006 6:40 PM
VJ
http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

VJ

Show quote
"Tim Johnson" <TimJohn***@discussions.microsoft.com> wrote in message
news:F79095F0-6E80-4B79-9228-30915DEDC998@microsoft.com...
>I know I can do this:
>
> string value = myenum.enum1.ToString();  //= "myenum1"
>
> but how can I do the reverse?  Given a string "myenum1", how do I turn it
> into myenum.enum1?  I was hoping there's some more clever way than this:
>
> myenum x = value=="myenum1" ? myenum.enum : ...;
Author
19 Apr 2006 6:51 PM
Tim Johnson
Thanks to all for the fast replies!

Show quote
"VJ" wrote:

> http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx
>
> VJ
>
> "Tim Johnson" <TimJohn***@discussions.microsoft.com> wrote in message
> news:F79095F0-6E80-4B79-9228-30915DEDC998@microsoft.com...
> >I know I can do this:
> >
> > string value = myenum.enum1.ToString();  //= "myenum1"
> >
> > but how can I do the reverse?  Given a string "myenum1", how do I turn it
> > into myenum.enum1?  I was hoping there's some more clever way than this:
> >
> > myenum x = value=="myenum1" ? myenum.enum : ...;
>
>
>

AddThis Social Bookmark Button