I am trying to assign a string to an enum. Something like the following example:
enum MyEnum
{
frist = "First Value",
second = "Second Value",
third = "Third Value"
}
So that I can have something like this in my code:
MyEnum enumVar = MyEnum.first;
...
string enumValue = EnumVar.ToString();//returns "First Value"
In conventional way when i create an Enum, the ToString() will return the enums name not its value.So it is not desirable since I am seeking a way to assign a string value and then get that string value out of an enum.
The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong. so Like a string the only thing you can to is
MyEnum.first.ToString();
or like using reflection and go using description as a attribute to the fields in the enum.
You may add a Description attribute to your enum.
Then have a method to return you the description.
Then you can do:
value will hold "First Value"
You may see: Associating Strings with enums in C#
if you need a full string like with spaces, etc, you need to go by adding the Description approach. Else I recomment just creating a lookup dictionary.
You can't do that.
enum
s are based on integers, not strings.You can't the value of an enum is always an integer
The closest you can come is a static class with a set of static properties