:::: MENU ::::

Monday, October 27, 2008

nums 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 framework

public 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;

    WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);

}