Enum Member
-
hi guys. Any one has idea about how can we give enum member like A+,B+...
public enum Color
{
Any,
D+,
D,
D-,
E+
};I tried creating above enum but bad luck its giving error saying } expected.
dipak
-
hi guys. Any one has idea about how can we give enum member like A+,B+...
public enum Color
{
Any,
D+,
D,
D-,
E+
};I tried creating above enum but bad luck its giving error saying } expected.
dipak
You can't. Enum members follow the normal rules for names - same as methods, properties, fields and namespaces. All you can do is use the word "Plus" or "Minus" instead.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
You can't. Enum members follow the normal rules for names - same as methods, properties, fields and namespaces. All you can do is use the word "Plus" or "Minus" instead.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
Does it mean that ther is no way to make such member in enum?
dipak
-
Does it mean that ther is no way to make such member in enum?
dipak
Yes. In C#, all identfiers (methods, properties, fields, and namespaces) must adhere to the following rules: 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. The characters '+', '-', '(' ')', '=', ',', ';' and many others cannot be used in the name of any identifier. So, no. You can't do it. At all. Ever.
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
-
hi guys. Any one has idea about how can we give enum member like A+,B+...
public enum Color
{
Any,
D+,
D,
D-,
E+
};I tried creating above enum but bad luck its giving error saying } expected.
dipak
You can only use underscores and alphnumeric characters in enum ordinal names. You also can't start the name with a digit.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
hi guys. Any one has idea about how can we give enum member like A+,B+...
public enum Color
{
Any,
D+,
D,
D-,
E+
};I tried creating above enum but bad luck its giving error saying } expected.
dipak
Add a Description attribute, and use reflection to extract the description from that e.g
[Description("D+")]
DPlusThere are numerous samples showing how to do this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Add a Description attribute, and use reflection to extract the description from that e.g
[Description("D+")]
DPlusThere are numerous samples showing how to do this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
thankyou so much ....all guys..
dipak
-
Add a Description attribute, and use reflection to extract the description from that e.g
[Description("D+")]
DPlusThere are numerous samples showing how to do this.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
That's what I would have said too. :thumbsup:
-
hi guys. Any one has idea about how can we give enum member like A+,B+...
public enum Color
{
Any,
D+,
D,
D-,
E+
};I tried creating above enum but bad luck its giving error saying } expected.
dipak
As Pete said, use a
System.ComponentModel.DescriptionAttribute
. See also my Enum Utilities[^] for ways to work with them more easily.