Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Humanizing the Enumerations

0.00/5 (No votes)
22 Mar 2005 1  
Convert enum'eration codes to the phrases your business customers use, with attributes and reflection in .NET.

Foreword

After day 1st being on CodeProject, this article got several interesting comments and I decided to go with Version 2, in order to make it more versatile than Version 1.

Introduction

Say, you have an ol' boring enum reflecting a Purchase Order status in your application's universe:

public enum PurchaseOrderStatus
{
    Opened,
    Consulting,
    InProgress,
    Finalizing,
    Closed,
    Canceled
}

How do you fill the ComboBox with items that communicate in simple terms the business logic behind each of those items? Whicever way you choose, consider the method described below..

Please note that this is more a sample of re-usable code than a tutorial, so I won't go deep into details on how reflection works and such.

Putting the Attributes and Reflection into Business

The approach is simple: each enum's member is being marked with an attribute that links the code with a human term's resource identifier in your application.

Here is how your enum will look like:

public enum PurchaseOrderStatus
{
    Opened,
    Consulting,
    InProgress,
    [HumanReadable("PO_Finalizing")]Finalizing,
    Closed,
    Canceled
}

Looks like nothing has changed. Except that one HumanReadable attribute has been added. But the helper code being described below assumes that you have strings with codes PurchaseOrderStatus.Opened, PurchaseOrderStatus.Consulting and so on, and PO_Finalizing in your resources file then uses reflection and substitutes the codes with human-readable information.

So, the rule is: define your enum; add strings with enum identifiers to your resource file; if there're already strings with such codes, override the codes with HumanReadable attribute; use helper class to convert your enum to human-readable information for data-aware controls.

Since Version 2, you're able not to have resource strings in your resources at all, and the helper code will use HumanReadable attribute for getting the human-readable strings for your enum values. This is helpful when you're developing an application for your internal use and not plannig to translate the values at all. Note that this is not recommended practice.

public enum PurchaseOrderStatus
{
    [HumanReadable("Just Opened")]Opened,
    [HumanReadable("Consulting with Our Partners")]Consulting,
    [HumanReadable("Still in Progress")]InProgress,
    [HumanReadable("Almost There")]Finalizing,
    [HumanReadable("Closed the Deal!")]Closed,
    [HumanReadable("Oops, Canceled")]Canceled
}

This approach doesn't use the standard Description attribute. The purpose of the Description attribute is to show you a detailed description of the method/property/event/whatever in the IDE designer, so better to leave this as is and to not to add additional responsibilities for this attribute.

Now What?

And now just make a call to the helper class method in order to get a map of enum values to strings loaded from resources:

//  Somewhere in the form initialization code...


ComboBox purchaseOrderStatus;

...

//  These can be set from within the IDE

purchaseOrderStatus.DisplayMember = "HumanReadableName";
purchaseOrderStatus.ValueMemeber = "Value";
//-


purchaseOrderStatus.DataSource = 
    EnumToHumanReadableConverter.Convert(typeof(PurchaseOrderStatus), 
    YourResourceManager);

How this Works?

EnumToHumanReadableConverter.Convert method obtains resource string identifiers from the enumeration using reflection and returns an ArrayList of HumanReadableName - Value pairs. ComboBox via its DataSource property consumes this list and uses DisplayMember and ValueMember properties to get a clue of what to display and what to return as SelectedValue.

public class HumanReadablePair
{
    public object Value
    {
        get;
        set;
    }

    public string HumanReadableName
    {
        get;
        set;
    }
}

Actually, you can set DisplayMember and ValueMember properties just from within the IDE, and only assign DataSource property in your code.

In order to select a proper value in the box, you can use the following snippet:

purchaseOrderStatus.SelectedValue = PurchaseOrderStatus.Finalizing;

The following code will retrieve the selected Purchase Order status from the ComboBox:

PurchaseOrderStatus selected = 
         (PurchaseOrderStatus)purchaseOrderStatus.SelectedValue;

YourResourceManager variable is the instance of the resource manager that will be used to load proper strings depending on their identifiers from the resources your application uses. The variable can be initialized once and be used in many places of your application. Such an approach makes the code globalization-aware.

ResourceManager YourResourceManager = 
    new ResourceManager("HumanizingTheEnumerations.Strings", 
    Assembly.GetExecutingAssembly());

Here's how your resource editor will look like:

Once again, the code that shows the enum values in ComboBox:

purchaseOrderStatus.DataSource = 
    EnumToHumanReadableConverter.Convert(typeof(PurchaseOrderStatus), 
    YourResourceManager);

If you do not plan to localize the UI of your application, you can use a simpler version of the Convert method:

purchaseOrderStatus.DataSource = 
    EnumToHumanReadableConverter.Convert(typeof(PurchaseOrderStatus));

Since Version 2, you're able to obtain human-readable string for a particular value of the enum:

labelTest.Text = 
    EnumToHumanReadableConverter.StringFor(InvoiceStatus.Processing, 
    YourResourceManager);

Or

labelTest.Text = 
    EnumToHumanReadableConverter.StringFor(InvoiceStatus.Processing);

All you need to start with the described approach is in the HumanReadableAttribute.cs file attached to the article.

Just Like That

Did I mention that it also works with ListBox? With all data-aware controls, I guess.

History

  • Version 2.0 so far.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here