I've seen information on this technique in several places, possibly even on this forum. I've extended what I've read by creating a couple of classes that make displaying friendly names easier.
First, a bit of background. Enumerations, created in C# with the 'enum' key word, are lists of text values that each correspond to a number. They are used to provide a limited list of possible values. We use them in many situations, for instance the DialogResult values that are returned by MessageBox.Show are members of the DialogResult enumeration. The idea is that the text labels are descriptive and make life easier for the developer, while the system uses the underlying numeric values for speed and efficiency. Enumerations are better than either String or Integer when you want to limit the possible choices a variable can take.
I say that the text labels are friendly to the developer, but what about the user? The text labels used for enumerated constants must obey the same rules as other identifiers. That means that they must start with a letter or an underscore and can only contain letters, numbers and underscores. They cannot contain spaces or punctuation. That makes them a little less friendly to display to the user in the UI. It would be nice if we could associate a standard String with each value and display those to the user. Well, we can do that, using the DescriptionAttribute class:
CSharp Code:
using System.ComponentModel;
public enum TestEnum
{
[Description("This is the first value")]
FirstValue,
[Description("Hello from the second value")]
SecondValue,
[Description("Third value here")]
ThirdValue
}
Note the spaces in the descriptions that are not allowed in the value names themselves. Now, the question is, how do we actually get those descriptions in code? The answer is to use reflection. In other places I've seen this done with a method at the form level or the like. I've been a good little OOPer and created a couple of classes to do the job. The following EnumDescriptor class encapsulates the reflection code, providing access to an enumerated value and its description via properties:
CSharp Code:
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// Contains an enumerated constant value and a friendly description of that value, if one exists.
/// </summary>
/// <typeparam name="T">
/// The enumerated type of the value.
/// </typeparam>
public class EnumDescriptor<T>
{
/// <summary>
/// The friendly description of the value.
/// </summary>
private string _description;
/// <summary>
/// The enumerated constant value.
/// </summary>
private T _value;
/// <summary>
/// Gets the friendly description of the value.
/// </summary>
/// <value>
/// A <b>String</b> containing the value's Description attribute value if one exists; otherwise, the value name.
/// </value>
public string Description
{
get
{
return this._description;
}
set
{
this._description = value;
}
}
/// <summary>
/// Gets the enumerated constant value.
/// </summary>
/// <value>
/// An enumerated constant of the <b>EnumDescriptor's</b> generic parameter type.
/// </value>
public T Value
{
get
{
return this._value;
}
set
{
this._value = value;
}
}
/// <summary>
/// Creates a new instance of the <b>EnumDescriptor</b> class.
/// </summary>
/// <param name="value">
/// The value to provide a description for.
/// </param>
public EnumDescriptor(T value)
{
this._value = value;
// Get the Description attribute.
FieldInfo field = value.GetType().GetField(value.ToString());
So, there's no need for you to mess around each time you want to do this once you have these classes. It's one line to create a collection containing the enumerated values and their descriptions, which you can bind or use however you like.
I've attached code files for the EnumDescriptor and EnumDescriptorCollection classes.
Last edited by jmcilhinney; Jan 28th, 2010 at 02:22 AM.