views:

68

answers:

3

The application I'm working on has lots of enumerations.

These values are generally selected from drop downs in the application.

What is the generally accepted way of storing the string description of these values?

Here is the current issue at hand:

Public Enum MyEnum
   SomeValue = 1
   AnotherValue = 2
   ObsoleteValue = 3
   YetAnotherValue = 4
End Enum

The drop down should have the following selections:

Some Value
Another Value
Yet Another Value (Minor Description)

Not all fit the name of the enumeration, (minor description on one is an example), and not all enumeration values are -current- values. Some remain only for backwards compatibility and display purposes (ie printouts, not forms).

This leads to the following code situation:

For index As Integer = 0 To StatusDescriptions.GetUpperBound(0)
   ' Only display relevant statuses.
   If Array.IndexOf(CurrentStatuses, index) >= 0 Then
      .Items.Add(New ExtendedListViewItem(StatusDescriptions(index), index))
   End If
Next

This just seems like it could be done better, and I'm not sure how.

+4  A: 

You could use the Description attribute (C# code, but it should translate):

public enum XmlValidationResult
{
    [Description("Success.")]
    Success,
    [Description("Could not load file.")]
    FileLoadError,
    [Description("Could not load schema.")]
    SchemaLoadError,
    [Description("Form XML did not pass schema validation.")]
    SchemaError
}

private string GetEnumDescription(Enum value)
{
    // Get the Description attribute value for the enum value
    FieldInfo fi = value.GetType().GetField(value.ToString());
    DescriptionAttribute[] attributes = 
        (DescriptionAttribute[])fi.GetCustomAttributes(
            typeof(DescriptionAttribute), false);

    if (attributes.Length > 0)
    {
        return attributes[0].Description;
    }
    else
    {
        return value.ToString();
    }
}

Source

ChrisF
Thanks for the source link, gives good ideas for further expansion of this capability.
Aequitarum Custos
+1  A: 

The most common way I've seen is to annotate your enums with the System.ComponentModel.DescriptionAttribute.

Public Enum MyEnum

   <Description("Some Value")>
   SomeValue = 1
...

Then to fetch the value, use an extension method (excuse my C#, I'll convert it in a minute):

<System.Runtime.CompilerServices.Extension()> 
Public Function GetDescription(ByVal value As Enum) As String
   Dim description As String =  String.Empty 

   Dim fi As FieldInfo =  value.GetType().GetField(value.ToString()) 
   Dim da = 
       CType(Attribute.GetCustomAttribute(fi,Type.GetType(DescriptionAttribute)),
                                             DescriptionAttribute)

   If da Is Nothing
      description = value.ToString()
   Else
      description = da.Description
   End If

   Return description
End Function

That's my best stab at converting it to VB. Treat it as pseudocode ;)

womp
This works, if localization isn't needed.
driis
+1  A: 

I would put them in a resource file, where the key is the enumeration name, possibly with a prefix. This way you can also localize the string values easily.

driis