The Wonderful World of Enums
Monday, October 26, 2009 11:56:00 AM
I am often asked various questions on enums and thought I would answer some quick questions here.
Q: When I create an enum, but do not give it a numeric representation, does that numeric representation still exist?
A: Yes. By default, an enum has a numeric “pointer” (if you will) – even if you don’t explicedly set it.
Take the following enums for example:
enum test1Values
{
Item1,
Item2
}
enum test2Values
{
Item1 = 0,
Item2 = 1
}
By default these two enums are the same. If you were to do a Convert.ToInt32() on item1 of the enum, both test1Values.item1 and test2Values.item1 would give you the value of 0.
Q: If I do a .ToString() on an enum value, what do I get back?
A: You get back the string test of the enum.
Take the following enum for example:
enum test1Values
{
Item1,
Item2
}
If you do a test1Values.item1.ToString(), you get back “item1”.
Q: If I have text string that matches an enum, how can I convert that string to the enum type? Another variation of the question is… I have a data object with an enum property and I want to cast a database string value to the corresponding enum type – how do I do that?
A: You can cast a string to an enum using the System.Enum.Parse method.
Take the following enum for example:
enum test1Values
{
Item1,
Item2
}
If you have a string of “Item1” and want to cast it to test1Values.Item1, you can do the following:
//convert string to enum type. I could have just converted the orig string, but I wanted to make sure I had a match
String myStringVal = “Item1”;
test1Values myVal =(test1Values)System.Enum.Parse(typeof(test1Values), myStringVal);
If you wanted to do a bit of error checking before you did your cast, you could either to a try catch or do a compare such as the following:
String myStringVal = “Item1”;
test1Values myVal;
//test to see if our string really matches our enum value
if (myStringVal == test1Values.Item1.ToString())
{
//convert string to enum type. I could have just converted the orig string, but I wanted to make sure I had a match
myVal =(test1Values)System.Enum.Parse(typeof(test1Values), myStringVal);
}
Q: Can I bind an enum to a dropdown list?
A: Yes you can. The System.Enum class provides a method that returns an String array of the enum values.
Take the following enum for example
public enum Size
{
Small,
Medium,
Large
}
protected System.Web.UI.WebControls.DropDownList lstSize;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
lstSize.DataSource = Enum.GetNames(typeof(Size));
lstSize.DataBind();
}
}
Of course, to get the selected enum back from the dropdownlist value, you would use the Enum.Parse method I showed earlier:
Size selectedSize = (Size)Enum.Parse(lstSize.SelectedValue);
Q: How can I loop through the values of an enum?
A: You can loop through an enum in the same manner that you use to bind an enum to a list.
Take the following enum for example
public enum Size
{
Small,
Medium,
Large
}
String[] mySizes = Enum.GetNames(typeof(Size));
for (Int32 x = 0; x < mySizes.Length; x++)
{
String mySizeAsString = mySizes[x];
Size mySizeAsEnum = (Size)System.Enum.Parse(typeof(Size), mySizes[x]);
}
These enum examples are true of all enums, whether they be custom enums or intrinsic enums such as System.Drawing.Color.