A while back I posted about using an enum as a datasource for your drop-down lists, see Strongly-Type a DropDownList with Enumerated Types. As it turns out, the big drawback to this approach is that your DropDownList's text is limited to the text that you use in your enum. This is fine if your text doesn't have any spaces, or other special characters, since enumerations don't allow these. But most of the time, you'll need a more presentable version of your text to show to your end-users.
One approach would be to create a method to convert your enumerated value to text, switching on the value. This isn't the best approach, however, because you've now logically separated the original enum from it's text representation, which can lead to some problems with maintenance. Also the code for doing this can get ugly, and really should probably be relegated to The Daily WTF.
Fortunately, there's a better way: Attributes. I've been reading Applied .NET Attributes, by Jason Bock and Tom Barnaby recently, and attributes are a perfect solution to this problem. They keep the meaning of the enum together with the definition, and the code to access this information is simple and straight forward. So, here's the code from the last post, re-written to use attributes:
The new enumeration, with the Description attribute:
public enum TShirtSize
{
[Description("Small (size 0-5)")] Small = 0,
[Description("Medium (size 6-10)")] Medium = 1,
[Description("Large (size 10-13)")] Large = 2,
[Description("Extra-Large (sizes 13 and up)")] ExtraLarge = 3
}
A helper method to reflect and get the Description:
public static string GetDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length>0) ? attributes[0].Description : value.ToString();
}
And finally, our new ddl code:
DropDownList ddl = new DropDownList();
foreach(TShirtSize siz in Enum.GetValues(typeof(TShirtSize)))
{
ddl.Items.Add(
new ListItem(GetDescription(siz), Convert.ToInt32(siz).ToString())
);
}
For more on this, see this great article, Mapping Text to Enum entries By Reto Ravasio
-Brendan
Posted
12-08-2004 7:07 AM
by
Brendan Tompkins