Alex Rivera | Logout

What's the best practice way to convert enum to string?

Asked 2010-03-31T21:57:54.227
9

I have enum like this:

public enum ObectTypes
{
    TypeOne,
    TypeTwo,
    TypeThree,
    ...
    TypeTwenty
 }

then I need to convert this enum to string. Now Im doing this that way:

public string ConvertToCustomTypeName(ObjectTypes typeObj)
{
    string result = string.Empty;
    switch (typeObj)
    {
        case ObjectTypes.TypeOne: result = "This is type T123"; break;
        case ObjectTypes.TypeTwo: result = "Oh man! This is type T234"; break;
        ...
        case ObjectTypes.TypeTwenty: result = "This is type last"; break;
    }

    return result;
}

Im quite sure that there is better way do do this, Im looking for some good practice solution.

EDIT: There is no one pattern in result string.

Thanks in advance.

Edit
Report

2 Answers

19

I use the [Description] attribute from System.ComponentModel

Example:

public enum RoleType
{
    [Description("Allows access to public information")] Guest = 0,
    [Description("Allows access to the blog")] BlogReader = 4,
}

Then to read from it I do

public static string ReadDescription<T>(T enumMember)
{
    var type = typeof (T);

    var fi = type.GetField(enumMember.ToString());
    var attributes = (DescriptionAttribute[]) 
            fi.GetCustomAttributes(typeof (DescriptionAttribute), false);
    return attributes.Length > 0 ? 
        attributes[0].Description : 
        enumMember.ToString();
}

Then usage

ReadDescription(RoleType.Guest);

Note: this solution assumes a single culture application as nothing was specifically asked about multiple cultures. If you are in a situation that you need to handle multiple cultures I would use the DescriptionAttribute or similar to store a key to a culture aware resource file. While you could store the enum member directly in the .resx file that would create the tightest coupling possible. I see no reason why you would want to couple the internal workings of your application (the enum member names) to key values that exist for internationalization purposes.

answered 2010-03-31T22:00:01.320
1

I would put these values in a database. Roles usually belong in a database for several reasons including these:

  1. Reports may need to be run against the roles, thus a need to display the descriptions.
  2. Role definitions can be configured without deploying new code.
  3. Roles should be enforced with a key constraint.

If done correctly, performance should not be an issue getting the roles from the database. Additionally, because a role definition table rarely changes, it is a very good candidate for caching.

If you find yourself having to hack around an Enum to get it to work, maybe you shouldn't use an Enum. Given the proper placement, everything should flow much better in code with little or no hacks. Granted Enums have their place; for example, a rather static mutually exclusive status.

answered 2011-01-18T18:01:48.703

Your Answer