Wednesday, June 08, 2011

C# - Enum to String

The idea was to have an enumerate and use its elements as strings instead of values.

public enum ButtonNames
{
SummaryBtn,
UpdatedBtn,
ToUpdateBtn,
NotInstalledBtn
}


For using the values - an example to write in console:

Console.WriteLine(Enum.GetName(typeof(ButtonNames), ButtonNames.NotInstalledBtn));


Another way

ConsoleWriteLine(ButtonNames.NotInstalledBtn.ToString());


The use in a switch element is not allowed since the enumerates are not constant values... that was my main objective. Will keep trying to find a way.


Hope it helps,


References:

Convert an Enum to a String By Mahesh Chand

No comments: