Enum parsing in C#
Frequently, you will have a situation where you store the string representation of an enumerated value in a file or database, and when it is retrieved you have to re-assign it to a variable that is of the enumerated type. You can do this as follows:
public enum MyEnumeratedType
{
Foo = 0, Bar = 1
}
MyEnumeratedType enumVariable = (MyEnumeratedType) Enum.Parse(typeof(MyEnumeratedType), “Bar”, true);
With all due respect to Anders, this is NUTS!!! I wish there was a simpler way…