tags:

views:

166

answers:

6

Hopefully a quicky.

This is a valid enum

public enum myEnum
{
  a= 1,
  b= 2,
  c= 3,
  d= 4,
  e= 5,
  f= 6,
  g= 7,
  h= 0xff
};

But this is not

public enum myEnum
{
  1a = 1,
  2a = 2,
  3a = 3,
};

Is there a way I can use an number in a enum. I already have code to populate dropdowns from enums so it would be quite handy

+5  A: 

No, there isn't. C# does not allow identifiers to start with a digit.

Application usability note: In your application you should not display code identifiers to the end-user anyway. Think of translating individual enumeration items into user-friendly displayable texts. Sooner or later you'll have to extend the enum with an item whose identifier won't be in a form displayable to the user.

UPDATE: Note that the way for attaching displayable texts to enumeration items is being discusses, for example, here.

Ondrej Tucny
You can use DescriptionAttribute to associate a user-friendly string with each enum value and populate your dropdowns by extracting the attribute values instead of showing the raw enums.
John Bowen
+3  A: 

No way. A valid identifier (ie a valid enumeration member) cannot start with a digit.

Noe
+2  A: 

Enumerations are no different than variables in terms of naming rules. Therefore, you can't start the name with a number. From this post, here are the main rules for variable naming.

  • The name can contain letters, digits, and the underscore character (_).

    • The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended at the beginning of a name. An underscore is often used with special commands, and it's sometimes hard to read.

    • Case matters (that is, upper- and lowercase letters). C# is case-sensitive; thus, the names count and Count refer to two different variables.

    • C# keywords can't be used as variable names. Recall that a keyword is a word that is part of the C# language. (A complete list of the C# keywords can be found in Appendix B, "C# Keywords.")

keyboardP
Better yet, the C# Language Specification section on Identifiers: http://msdn.microsoft.com/en-us/library/aa664670(v=VS.71).aspx
Jim Mischel
Agreed, nice link!
keyboardP
+4  A: 

No identifier at all in C# may begin with a number (for lexical/parsing reasons). Consider adding a [Description] attribute to your enum values:

public enum myEnum
{
    [Description("1A")]
    OneA = 1,
    [Description("2A")]
    TwoA = 2,
    [Description("3A")]
    ThreeA = 3,
};

Then you can get the description from an enum value like this:

((DescriptionAttribute)Attribute.GetCustomAttribute(
    typeof(myEnum).GetFields().Single(x => x.GetValue(null) == enumValue),    
    typeof(DescriptionAttribute))).Description
Kirk Woll
perfect! Thatll get me out of a spot
DrLazer
+2  A: 

Identifiers can't start with numbers. However, they can contain numbers.

Rohan Singh
+1  A: 

An identifier in C# (and most languages) cannot start with a digit.

If you can modify the code that populates a dropdown with the enumeration names, you could maybe have a hack that strips off a leading underscore when populating the dropdown and define your enum like so:

public enum myEnum
{
  _1a = 1,
  _2a = 2,
  _3a = 3
};

Or if you don't like the underscores you could come up with your own 'prefix-to-be-stripped' scheme (maybe pass the prefix to the constructor or method that will populate the dropdown from the enum).

Michael Burr