Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

c#

enums

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.

like image 265
Dariusz Avatar asked Mar 31 '10 21:03

Dariusz


2 Answers

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.

like image 71
Chris Marisic Avatar answered Sep 17 '22 15:09

Chris Marisic


If you need a custom string, the best option would be to make a Dictionary< ObjectTypes, string>, and just do a dictionary lookup.

If you're fine with the default ToString() functionality, just use typeObj.ToString();

For the dictionary approach, you could do:

private static Dictionary<ObjectTypes, string> enumLookup;

static MyClass()
{
    enumLookup = new Dictionary<ObjectTypes, string>();
    enumLookup.Add(ObjectTypes.TypeOne, "This is type T123");
    enumLookup.Add(ObjectTypes.TypeTwo, "This is type T234");
    // enumLookup.Add...

}

Your method becomes:

public string ConvertToCustomTypeName(ObjectTypes typeObj)
{
     // Shouldn't need TryGetValue, unless you're expecting people to mess  with your enum values...
     return enumLookup[typeObj];
}
like image 45
Reed Copsey Avatar answered Sep 18 '22 15:09

Reed Copsey