|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Convert string to enumI 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 : ...; Check out the following article. It's really not too hard to do:
http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx -- Show quoteHTH, Kevin Spencer Microsoft MVP Professional Numbskull Hard work is a medication for which there is no placebo. "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 : ...; Tim Johnson <TimJohn***@discussions.microsoft.com> wrote:
> I know I can do this: MyEnum foo = Enum.Parse (typeof (MyEnum), value);> > 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 : ...; -- 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
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 : ...; 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 : ...; > > > |
|||||||||||||||||||||||