4

HI
I have the following enum

public enum Priority : byte 
    {
        A=1,
        B+ = 2,
        B=4,
        C=8,
        D=16,
        E=32
    }

I want to add B+ in the enum but it is giving me error

Tassadaque
  • 8,129
  • 13
  • 57
  • 89
  • Of course, `B+` is not a valid name for a enum value. In fact it's not even a valid name for an identifier. What is your actual issue? – Etienne de Martel Jan 02 '12 at 06:05
  • if you need custom text for enum members try http://stackoverflow.com/questions/1187085/string-to-enum-conversion-in-c-sharp – Shoaib Shaikh Jan 02 '12 at 06:07
  • You can add user friendly description for enum like done [here] [here]:http://stackoverflow.com/questions/1331487/how-to-have-userfriendly-names-for-enumerations – Upendra Chaudhari Jan 02 '12 at 06:10
  • Another answer to this question: http://stackoverflow.com/questions/424366/c-sharp-string-enums – Ilya Kogan Jan 02 '12 at 06:14

4 Answers4

17

You can add user friendly description for enum like below :

enum MyEnum 
{ 
    [Description("This is black")] 
    Black, 
    [Description("This is white")] 
    White 
} 

Ref. Link : How to have userfriendly names for enumerations?

Community
  • 1
  • 1
Upendra Chaudhari
  • 6,473
  • 5
  • 25
  • 42
5

How about using a valid identifier like B_Plus?

Gary
  • 5,642
  • 1
  • 21
  • 41
2

Yes. It's giving you an error because your code is wrong. You can't make "B+" an enum value because there's a plus sign. Same reason you can't declare int B+. Use a different name.

Dan
  • 10,531
  • 2
  • 36
  • 55
0

You won't be able to use + as a name identifier because it's a math operator or string concatenator... it can't be used with enums. Use an alternative syntax, or use an alternative approach. You could consider a state design pattern:

http://www.dofactory.com/patterns/PatternState.aspx#_self2

Brian Mains
  • 50,520
  • 35
  • 148
  • 257