Converting Strings to Enums
Enums are great. They provide additional type safety and make code more readable.
Converting enums to strings is trivial. Converting strings to enums is not as straightforward...so here is a code snippet:
// This enum is in the .Net frameworkpublic
enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
}
protected void Button1_Click(object sender, EventArgs e)
{
// converting enums to strings is easy
String WhatDayItIs = DayOfWeek.Monday.ToString();
// converting strings to enums is a bit more work
DayOfWeek WhatDayItIsDOW;
if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs))
WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);
}
I hope you find this information useful.
Steve Wellens