Etiketler

Meta

  • Valid XHTML 1.0 Transitional
  • Valid CSS!
  •  
How to convert a String to an Enum?
object Enum.Parse(System.Type enumType, string value, bool ignoreCase);

enum Colour { Red, Green, Blue }
// ...
Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);
Console.WriteLine("Colour Value: {0}", c.ToString());

// Picking an invalid colour throws an ArgumentException. To
// avoid this, call Enum.IsDefined() first, as follows:
string nonColour = "Polkadot";
if (Enum.IsDefined(typeof(Colour), nonColour))
c = (Colour) Enum.Parse(typeof(Colour), nonColour, true);
else
MessageBox.Show("Uh oh!");

Thanx to Tim Sneath



etiketler : .net