Enum question

  • Thread starter Thread starter ChInKPoInt [No MCSD]
  • Start date Start date
C

ChInKPoInt [No MCSD]

Say, if I got this enum defined:

enum Colors { Red = 1, Green = 2, Blue = 4 }

If someone passes me a string with value "Red" , how can I Cast it to enum
or convert the variable so that it returns Colors.Red?

string mystring = "Red";
 
Hi,

You can use the Parse method of the Enum class. Try this:

string mystring = "Red";
Colors myEnum = (Colors)Enum.Parse(typeof(Colors),mystring);
Console.WriteLine("My Color is : {0}", myEnum.ToString());


Hope this helps.
 
Back
Top