patterncsharpCritical
Enum String Name from Value
Viewed 0 times
enumfromnamevaluestring
Problem
I have an enum construct like this:
In my database, the enumerations are referenced by value. My question is, how can I turn the number representation of the enum back to the string name.
For example, given
public enum EnumDisplayStatus
{
None = 1,
Visible = 2,
Hidden = 3,
MarkedForDeletion = 4
}In my database, the enumerations are referenced by value. My question is, how can I turn the number representation of the enum back to the string name.
For example, given
2 the result should be Visible.Solution
You can convert the
int back to an enumeration member with a simple cast, and then call ToString():int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();Code Snippets
int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();Context
Stack Overflow Q#309333, score: 786
Revisions (0)
No revisions yet.