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?

Community
  • 1
  • 1
Jacek
  • 11,661
  • 23
  • 69
  • 123
  • what u actually need? string "TELESALES" or the Value stored for this enum variable? – mihirj Jan 25 '13 at 15:12
  • I want to have something like two values for element byte and string to represent it on the screen – Jacek Jan 25 '13 at 15:14

3 Answers3

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();
}
Dave Zych
  • 21,581
  • 7
  • 51
  • 66
  • 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. – Ann L. Jan 25 '13 at 15:17
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");
Ann L.
  • 13,760
  • 5
  • 35
  • 66
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.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • IMO, this is not as maintainable as the technique that uses the `DescriptionAttribute`s. 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. – Cᴏʀʏ Jan 25 '13 at 16:27
  • @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. – Sergey Berezovskiy Jan 25 '13 at 16:37