Skip to main content
  1. Blog/

Converting string to Enum value

·1 min

Questa posted a very usefull piece of code…
It’s possible to convert a string representation of an enum back to an enum!

private enum MyEnumeration  
{
  FooBar,  
  Foo,  
  Bar
}

...  

MyEnum foo = MyEnum.Foo;  
foobar = foo.ToString();  
MyEnum bar = (MyEnum)Enum.Parse(typeof(MyEnum), foobar);

At the end of this code, the variable bar has the MyEnum value Foo, just the way it’s supposed to be…