0

Possible Duplicate:
C# Enums - can my enums have friendly names?
Cast string to enum with enum attribute

I have enum with byte values:

enum MarketingEventType : byte {MARKETING_CAMPAIGN, TELESALES, MARKETING_ACTIONS};

I would like to give for all element name, which I will get by ToSting() method. For example:

MarketingEventType.TELESALES.ToString(); // I get "bla bla bla"
MarketingEventType.MARKETING_ACTIONS.ToString(); // I get "la la la"

It is possibe without change type of enum from BYTE to STRING?

2
  • what u actually need? string "TELESALES" or the Value stored for this enum variable? Commented Jan 25, 2013 at 15:12
  • I want to have something like two values for element byte and string to represent it on the screen Commented Jan 25, 2013 at 15:14

3 Answers 3

6

You can't set an enum type to string. Valid base types are byte, sbyte, short, ushort, int, uint, long, and ulong.

You can, however, use the Description attribute:

enum MarketingEventType
{
    [Description("bla bla bla")]
    TELESALES,
}

Retrieving the enum description is kind of a mess, but you can use this method (or even make an extension method out of it!):

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(
        typeof(DescriptionAttribute),
        false);

    if (attributes != null &&
        attributes.Length > 0)
        return attributes[0].Description;
    else
        return value.ToString();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is cool. Since it's possible to add extension methods to an Enum, I'd rewrite this as an extension method so the OP can simply call a different instance method (or appear to) to get the desired value.
0

If all you want is "MARKETING" and "TELESALES" then you would perform

MarketingEventType.TELESALES.ToString();

... as you have in the question.

If you want the underlying byte values "0" and "1", you would perform

MarketingEventType.TELESALES.ToString("d");

Comments

0

Why not simply create method which will return string for your enum value?

public string GetMarketingEventName(MarketingEventType eventType)
{
    switch(eventType)
    {
        case MarketingEventType.MARKETING_CAMPAIGN: return "blah blah blah";
        case MarketingEventType.TELESALES: return "lalala";
        case MarketingEventType.MARKETING_ACTIONS: return "boo";
        default:
            throw new ArgumentException("eventType");
    }
}

Why would you dot that? Because you need string to be displayed on UI, and it's good practice to separate UI from domain. So, I think such method on controller class is better that attributes in your domain. Also consider about localization.

2 Comments

IMO, this is not as maintainable as the technique that uses the DescriptionAttributes. You have to remember to modify this function if you alter the enum, whereas with attributes, the maintenance is simple; it's staring you in the face.
@Cory I think it's OK to modify UI when you are modifying underlying model. You also can forget to add string resources for new enum values. And you can forget to add something specific for new marketing event type on UI (e.g. change control background color). You don't need to put UI stuff to model just because someone can forget to update UI.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.