Object reference not set to an instance of an object

Ruslan's ASP .NET weblog

string to enum

How do you convert a string into an enumeration?

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

example:

enum Colour

{

Red,

Green,

Blue

}

Colour c = (Colour) Enum.Parse(typeof(Colour), "Red", true);

Console.WriteLine("Colour Value: {0}", c.ToString());

 

source: http://blogs.msdn.com/tims/archive/2004/04/02/106310.aspx

Comments

Wim Hollebrandse said:

Dude - why regurgitate stuff that was posted back in 2004? Besides, it's hardly anything worth posting.

# April 24, 2008 4:24 PM

Miron said:

The method Enum.Parse will throw an exception if the enum doesn't have such value. Take a look at this post:

mironabramson.com/.../Another-version-for-the-missing-method-EnumTryParse.aspx

# April 25, 2008 8:56 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)