C# – Converting a string to an enum in C#


Possible Duplicate:
Converting a string to an enumeration value in C#

How do I convert a enum to a string in C#?

Note: I have the answer and will post, I searched for the answer here first but couldn't find it so I thought I would add the question / answer to the site once I found it.

Best Solution

Converting is actually quite easy. You would use the following function that is built in:

object Enum.Parse(System.Type enumType, string value, bool ignoreCase);


// To convert from a string to a enum just do the following
string sTestEnum = "VALUE2";

TEST_ENUM eDatabase = (TEST_ENUM)(Enum.Parse(typeof(TEST_ENUM), sTestEnum, true));