1

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.

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162
  • Exact duplicate: http://stackoverflow.com/questions/16100/converting-a-string-to-an-enumeration-value-in-c – Noldorin Jun 23 '09 at 15:52
  • I voted to close "exact duplicate". Please see http://stackoverflow.com/questions/16100/converting-a-string-to-an-enumeration-value-in-c/16104 – Andrew Hare Jun 23 '09 at 15:53
  • Totally agree no idea why it didn't come up in my initial search... I will delete. – Kelsey Jun 23 '09 at 15:55
  • FYI search string used: "string to enum in c#" Any idea why it wouldn't come and why the check while writing the subject didn't turn it up? – Kelsey Jun 23 '09 at 15:57
  • Question is: why doesn't question 16100 even show up in the Related sidebar? – p.campbell Jun 23 '09 at 20:15

1 Answers1

1

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);

enum TEST_ENUM
{
  VALUE1,
  VALUE2
}

// 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));
Kelsey
  • 47,246
  • 16
  • 124
  • 162