1

I have the following codes that I need to put into an enum:

 10, 11, 13, AA, BB, EE

I'm having a hard time trying to get them into an enum in C#. I currently have this:

 public enum REASON_CODES { _10, _11, _13, AA, BB, CC }

but would like to have this, with the numbers being recognized as strings, not ints:

 public enum REASON_CODES { 10, 11, 13, AA, BB, CC }

Is this possible or am I dreaming?

Captain Skyhawk
  • 3,499
  • 2
  • 25
  • 39
  • 4
    Enum items cannot begin with a number, so no, you cannot. – DonBoitnott Jun 18 '13 at 14:16
  • 1
    This might be of some help: http://stackoverflow.com/a/5299694/639960 – Akhil Jun 18 '13 at 14:18
  • 1
    Perhaps you should read up on what `Enums` are and how they are used and what their `Ordinal` position represents the key word / give away in this statement is `Ordinal` [Enums - MSDN](http://msdn.microsoft.com/en-us/library/sbbt4032(v=vs.80).aspx) – MethodMan Jun 18 '13 at 14:18
  • Unfortunately it has to be this way due to the data that is incoming. We're parsing codes that have mixed numbers and strings. I was pretty sure you couldn't make c# recognize the int as a string but knew there were some pretty clever developers that might know another way. – Captain Skyhawk Jun 18 '13 at 14:23
  • 1
    @CaptainSkyhawk Perhaps you're looking for a `Dictionary` instead then – Alvin Wong Jun 18 '13 at 14:24
  • @AlvinWong: There are a finite amount of them and unfortunately these codes are used all over the place. I don't want to have other developers having to remember these codes by memory or have to look them up. Much easier/quicker to have code completion give you a choice. – Captain Skyhawk Jun 18 '13 at 14:26

3 Answers3

5

Try using a enum and the DescriptionAttribute:

public enum REASON_CODES
{
    [Description("10")]
    HumanReadableReason1,
    [Description("11")]
    SomethingThatWouldMakeSense,
    /* etc. */
}

Then you can use a helper (like Enumerations.GetDescription) to get the "real" value (while staying within the C# naming constraints).

To make it a full-out answer:


Just in case anyone wants an extensions class that marries those two posters:

public static class EnumExtensions
{
    public static String ToDescription<TEnum>(this TEnum e) where TEnum : struct
    {
        var type = typeof(TEnum);
        if (!type.IsEnum) throw new InvalidOperationException("type must be an enum");

        var memInfo = type.GetMember(e.ToString());
        if (memInfo != null & memInfo.Length > 0)
        {
            var descAttr = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (descAttr != null && descAttr.Length > 0)
            {
                return ((DescriptionAttribute)descAttr[0]).Description;
            }
        }
        return e.ToString();
    }

    public static TEnum ToEnum<TEnum>(this String description) where TEnum : struct
    {
        var type = typeof(TEnum);
        if (!type.IsEnum) throw new InvalidOperationException("type must be an enum");

        foreach (var field in type.GetFields())
        {
            var descAttr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (descAttr != null && descAttr.Length > 0)
            {
                if (((DescriptionAttribute)descAttr[0]).Description == description)
                {
                    return (TEnum)field.GetValue(null);
                }
            }
            else if (field.Name == description)
            {
                return (TEnum)field.GetValue(null);
            }
        }
        return default(TEnum); // or throw new Exception();
    }
}

Then:

public enum CODES
{
    [Description("11")]
    Success,
    [Description("22")]
    Warning,
    [Description("33")]
    Error
}

// to enum
String response = "22";
CODES responseAsEnum = response.ToEnum<CODES>(); // CODES.Warning

// from enum
CODES status = CODES.Success;
String statusAsString = status.ToDescription(); // "11"
Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

In C#, an Identifier must start with a letter or an underscore unfortunately. There is no way to achieve this. Your first solution is the closest you can get.

http://msdn.microsoft.com/en-us/library/aa664670(v=vs.71).aspx

Bas
  • 26,772
  • 8
  • 53
  • 86
2

Enum value names must follow the same naming rules as normal variables in C#.

kindly check this question Can my enums have friendly names ?

Community
  • 1
  • 1
ebram khalil
  • 8,252
  • 7
  • 42
  • 60