1

I'm creating this selectbox in a SharePoint web part and need to have a drop down with the current version so I need to use an Enum.

public enum SelectVersionEnum { 2010, 2007 };

Well you can see where it breaks, is there any way to use integers in a enum? Most of all I'd like to use

public enum SelectVersionEnum { 2010=14, 2007=12 };
Jakub Hampl
  • 39,863
  • 10
  • 77
  • 106
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • The answers below are correct. Take a moment to consider that even if C# allowed this, it's a *really* bad idea and a maintenance nightmare. This is the kind of code that would eventually be posted to the Daily WTF. – Clinton Pierce Mar 14 '11 at 14:26

5 Answers5

7

Enum members must be valid C# identifiers.
They cannot start with numbers.

Instead, you can use something like Office2007, Office2010 or V2007, V2010.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
7

No, you can not name enums with integer names.

An enum value name is a normal identifier and must follow the same rules as everything else.

You can, however, use:

public enum SelectVersionEnum
{
    Version2007 = 12,
    Version2010 = 14
}

Additionally, Enum.Parse can parse strings with integers into their corresponding enum value, even if value described in the string doesn't exist.

Try the following in LINQPad:

void Main()
{
    Enum.Parse(typeof(SelectVersionEnum), "12").Dump();
    Enum.Parse(typeof(SelectVersionEnum), "14").Dump();
    Enum.Parse(typeof(SelectVersionEnum), "2007").Dump();
}

public enum SelectVersionEnum
{
    Version2007 = 12,
    Version2010 = 14
}

The output:

Version2007
Version2010
2007

What do you think would've happened if you defined the following:

public enum SelectVersionEnum
{
    12 = 14,
    14 = 16
}

Does the string "14" now mean "12" or "14"?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
3

No, enum identifiers can't start with a numeric character.

Femaref
  • 60,705
  • 7
  • 138
  • 176
2

You can add an extension method to your enum like any other type.

So you could create an extension for your SelectVersionEnum to help you get the enum values in the right format...

public static class SelectVersionEnumExtension
{
     public static int Version(this SelectVersionEnum enumValue)
     {
         return 0; // Obviously you should return something meaningful here..
     }
}

This gives you a lot of flexibilty.

Joel Gauvreau
  • 3,586
  • 4
  • 29
  • 32
1

One way you could have numeric values associated with enums is by using the description attribute. For example you might have the enum:

[Serializable]
public enum SelectVersionEnum
{
    [Description("2007")]
    v2007,
    [Description("2010")]
    v2010
}

You could then write an extension method to get the numeric value you are looking for.

public static string Description(this Enum value)
{
    var type = value.GetType();

    var name = Enum.GetName(type, value);

    if (name != null)
    {
        if (type.GetField(name) != null)
        {
            var attr = Attribute.GetCustomAttribute(type.GetField(name), typeof(DescriptionAttribute)) as DescriptionAttribute;

            return attr != null ? attr.Description : name;
        }
    }

    return null;

} // end

You would use it like this:

var version = SelectVersionEnum.v2007.Description();
John Meyer
  • 2,296
  • 1
  • 31
  • 39