Converting string to Enum value
By rickvdbosch
- 1 minutes read - 64 wordsQuesta 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…