Ever wanted to treat strings like they were enumerations and put them
through a switch statement? Ever wanted to bind the elements of an Enumeration
to UI element but couldn't because the Enum names were not user
friendly? How about storing a string as an Int in a database
table and having that Int value strongly bound to a specific display string? The
utility class helps solve this problem by using Generics, Attributes, and
Reflection to expose a set of static methods that transition between user
friendly strings and Enum values.
Using the code
Of course, there's no real magic involved and C# doesn't intrinsically
support string enumerators. The trick
to achieving this effect is in the use of the
which we use to decorate the enumerator.
The example below shows a Country
enumeration with a few countries listed.
is used to give the enum value a user friendly display name.
Using the class is really simple as will be demonstrated in the code samples
to follow. When converting from a
string back to an enumeration, the Parse
TryParse methods will try to
parse the passed in string to return the enumerator. The difference between the two methods
is that Parse will throw and
if the string could not be parsed whereas
returns a boolean flag that indicates if the operation was successful. Otherwise, both methods are identical in
that they will first try to match the string to the enum value directly. If a match cannot be made then the
methods will look for a
to match the string against. As
illustrated in the example below, both "United States" and "United
States" will return the
Country.UnitedStates enum value.
string enumString = "United
Country enumValue = StringEnumerator<Country>.Parse(enumString);
match which is similar to Enum.Parse()
enumString = "UnitedStates";
enumValue = StringEnumerator<Country>.Parse(enumString);
enumString = "United States of America";
success = StringEnumerator<Country>.TryParse(enumString,
So the example
above showed how to parse a string, now let's take a look at some of the
methods that allow you to serialize an enumeration to a string or a
StringEnumerator class provides a
ToString method that will
serialize an enumeration value to a string. This method will look for a
DescriptionAttribute to return, and if
one is not found, it will simply call built-in
ToString method on the enum value to get the intrinsic
behavior back to the user.
Country enumValue = Country.UnitedStates;
string expected = "United States";
string actual = StringEnumerator<Country>.ToString(enumValue);
enumValue = Country.Canada;
expected = Country.Canada.ToString();
actual = StringEnumerator<Country>.ToString(enumValue);
The static methods
that return a collection work in a similar fashion to generate a collection of
strings from a given enumeration.
This technique can be used to bind an enumeration to a UI element.
enumValues = StringEnumerator<Country>.ToList();
comboBox.DataSource = enumValues;
Notes on Internationalization
Anybody who has had to internationalize (I18N) software below knows that hard-coding
strings that are used for display purposes is not possible. Although, in its current state the
StringEnumerator class does not
support I18N, it could with some minor modifications be adapted to supply
dynamic strings. One way to
accomplish this would be to assign resource identifiers in the
DescriptionAttribute in place of real
strings. Then modify the parsing
methods to lookup the real string value from a resource manager. This approach would then allow you to
map integer values stored in a database to display values regardless of the
local that the application is running within.