I’ve come across the need to covert a string to an enum value on numerous projects in the past, each time I google the answer, implement it and then completely forget! One solution I found was to construct a huge switch statement, this is obviously a bad idea. For one it’s a waste of energy typing it all out plus it won’t deal with any future changes to the enum. Instead have a look at the following code:
object Enum.Parse(System.Type enumType, string value, bool ignoreCase);
Here’s an example for a colour enum:
(Colour) Enum.Parse(typeof(Colour), "Green", true);
Easy huh!