Convert string to enum

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 said:
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);
 

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

Similar Threads

enum base class 5
Enum To String 14
How to convert string to enum ? 3
convert DataRow col value to enum type 1
Globalization & Custom Converters 1
enum basics 7
about enum 3
serialize custom-typed dictionary to xml 2

Back
Top