Reputation: 1468
I have created enum like the following,
public enum colors
{
Red = 1,
green = 2,
blue = 3
}
I have to use this enum into page wherever i save the colors into pages. After save i have to show which color i have saved.
But for me The result is showing like, eg: red means 1.
I have tried the following,
o.Colors != null ? ((Models.Enums.Colors)o.ColorMode) : 0
the above code i should get the red, green, blue. but i got the result like 1,2,3...
Please Help me..
Upvotes: 1
Views: 153
Reputation: 804
Try this code
string className = Enum.GetName(typeof(Models.Enums.Colors),
o.Colors != null ?((Models.Enums.Colors)o.ColorMode) : 0);
or use Generic Functions
private static T ToEnum(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
private string EnumToString(T enumValue)
{
Type typeParameterType = typeof(T);
return Enum.GetName(typeParameterType, enumValue);
}
For your situvation Dictionary is best option
Reason: If you want to add class with space or "-" then enum is scope less
// Use a dictionary with an int key.
Dictionary<int, string> StyleDict = new Dictionary<int, string>();
StyleDict.Add(1, "Red");
StyleDict.Add(2, "green ");
StyleDict.Add(3, "blue");
Then you can access This dictionary like below
StyleDict[1] //To get Red
StyleDict[2] //To get green
try for dynamic
StyleDict[o.ColorMode]
Upvotes: 6
Reputation: 1458
you may try this
public enum colors
{
Red = 1,
green = 2,
blue = 3
}
colors col = selectedCol; // selectedCol is the color you select for your Applications
object val = (col != null) ? Convert.ChangeType(col, typeof(string)) : 0;
val
now exactly has the result you need
Upvotes: 1
Reputation: 116
It's something like this. I didn't test it.
o.Colors != null ? ((Models.Enums.Colors)o.ColorMode).ToString() : 0
Upvotes: -3