Repurposing Enumerations
page 1 of 2
Published: 15 Jan 2004
Unedited - Community Contributed
This article highlights a case where a TypeConverter class was used to repurpose an enumeration.
by Mathew Nolton
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 14539/ 59
Article Contents:

Part 1

Repurposing Enumerations

Enumerations provide the ability to name and then group integral values. Using an enumeration a programmer can provide a set of named values for a variable, property, or parameter.  .NET has made enumerations even friendlier. Base functionality in the .NET Enum class lets you retrieve the names of an enumeration member, retrieve name values as an array, and bind them to controls as a data source. This functionality, coupled with the use of a TypeConverter class, allows a programmer to use enumerations in novel ways.


This article highlights a case where TypeConverter classes were used to repurpose enumerations in order to provide a friendly façade to a third-party API that expects parameterized string values.


Problem Description
Enumerations are useful because they provide the ability to put a friendly name to any integral value except char. Additionally, they are a design time construct so you can use dot “.” notation and Intellisense to interact with them. Such as:


// define it

public enum MyEnumeration






// use it

protected MyEnumeration localEnum=MyEnumeration.FriendlyName1;


However, there is not a similar construct for putting a friendly name to a set of constant string values. A typical implementation would be to define a set of static properties or public read-only string values on a class. Although a valid approach, it has a couple of limitations:

1.      An enumeration is a specialized typed value. A static property or constant string value is not. For example:

// example of specialized value using enum

// valid values are FriendlyName1 and FriendlyName2

public SomeMethod(MyEnumeration enumParameter)


    .. .. ..



// can’t do this with strings, any string value will do.

public SomeMethod(string stringParameter)


    .. .. ..



2.      If there is a limited set of valid string values, then you will need to write some special validation code. Additionally, the meaning behind the string values may or may not be easy to remember.

In order to make the points real, let’s discuss a real world situation. Through my company Cybral, I implement various .NET XML web service solutions. At one particular client, I am interacting with a third-party API to create new work orders in a billing system. This billing system requires a set of parameterized string values to define the type of work order being created. To make it really simple, let’s say there are just four valid values:

1.      "CSRVN" means change services and send a technician.

2.      "CSRVY” means change services but do not send a technician.

3.      "INSTN" means install services and send a technician.

4.      "INSTY" means install services but do not send a technician.

Reading through these codes, you soon realize that they are painful to use. Now multiply this problem by 100 because there are a large number of these situations.

Now, we could create four different functions on a class that expose these different tasks, using a friendly name for each method. For example, ChangeServiceNoTechnician () or ChangeServiceOfferingWithTechnician(). However, what if there are other parameter values that can be combined with these codes? Soon we would have so many methods that we would be creating more problems then we solve.

Enumerations are the answer. But remember these are string values, so how do we generically handle converting these enumerated values to values the underlying API can understand? If the converted value is an integral value, then the default Enum class can handle everything. If we must translate them to other string values, we will have to create a converter class of some sort. Fortunately for us, Microsoft has thought of this too and has provided the TypeConverter.

View Entire Article

User Comments

Title: Repurposing Enumerations - a different approach   
Name: Matteo
Date: 2008-01-14 7:22:24 AM

I came to this article months ago when I had to manage a similar situation.
It was very helpful for me, mainly for letting me know about TypeConverter and
It drove me in the right direction, though I chose a different approach.
I wanted to keep client code as simple as possible:

1. MyObject.Property = MyEnum.Item;
2. string humanReadable = MyObject.Property;

still being able to store in 'humanReadable' something different from the default string
representation of enum items.

I implemented a little helper class with a couple of conversion operators.
I published a description of my approach in my blog at the following URL:

and I would like you to visit and comment it if you can.

Thank you very much.


Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-28 11:31:29 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search