Display Text for Enum [duplicate] - c#

This question already has answers here:
String to enum conversion in C#
(8 answers)
C# using numbers in an enum
(8 answers)
Closed 9 years ago.
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

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?

How about using a valid identifier like B_Plus?

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.

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

Related

Type constant in c#? [duplicate]

This question already has answers here:
How to define constants in Visual C# like #define in C?
(7 answers)
Closed 7 years ago.
Is there an equivalent to this in C#?
#define TypeConstant int
int main()
{
TypeConstant x = 5;
}
Thank you very much!
Edit: I am not sure how this is related to defining a regular constant, I have explicitly written type constant, not a constant value! Read before you vote guys!
It's not exactly the same, but types can be aliased by using the using Directive:
using TypeConstant = System.Int32;
As Kyle points out in the comments, you need to use the full type name here (e.g. System.Int32) instead of the C# aliases (e.g. int).

How can specify new value for default system enum [duplicate]

This question already has answers here:
Can I add an enum to an existing .NET Structure, like Date?
(3 answers)
Closed 8 years ago.
I have one question
How can I specify new value into system enum?
For example, the LoginFailureAction has a enum that define with microsoft in c# how can I get new value into this enum?
Of course, you can't change the default value of a type. The type is the type, and you can't change it.

Override Tostring() of enum to accommodate runtime string changes [duplicate]

This question already has answers here:
How can I internationalize strings representing C# enum values?
(2 answers)
How do I have an enum bound combobox with custom string formatting for enum values?
(21 answers)
Enum ToString with user friendly strings
(25 answers)
Closed 10 years ago.
I have an enum like this
enum Animal:byte
{
Cat=0,
Dog=1,
Horse=2
}
I want to override the ToString() of it to to write a custom string because my application is a multi-language one(Cat.ToString() should retrieve the translated word of cat). so the Description keyowrd can't be used here.can anyone give me a hint to find a neat way to solve this?

C# Enum compiler bug/glitch? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.NET Enumeration allows comma in the last field
i noticed while i was refreshing my memory on c# that with enums, you dont get a complaint from the compiler when you leave a comma after the last variable... EG
enum fruit {
apple,
pear,
watermelon,
}
i was wondering you can do this? shouldnt the compiler say "syntax error: ," or something?
It is part of the C# specification, the compiler is simply following it.
Document: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf
Page 363, Section 19.7

C# Enum with description not working [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get Enum from Description attribute
Hi All,
I have and Enum defined like this.
public enum SomeType {
[Description("One Value")]
One,
[Description("Two Value")]
Two,
[Description("Three Value")]
Three
}
but when I try to parse a string like this
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three Value");
I get excetion "Requested value 'Three Value' was not found". Isn't this supposed to work ?
Thanks
No, it's not. You can find the Enum by the enum Name ("One", "Two", "Three"), but not by Description (at least not that way). Maybe via Reflection...
You might wanna take a look at this: How to get C# Enum description from value?
Update
Take a look at #KIvanov's comment and look here: Get Enum from Description attribute
As far as I know
SomeType test = (SomeType )Enum.Parse(typeof(SomeType ), "Three");
would do what you want

Categories

Resources