You are not Logged in
Would you like to Login or Register

Today is: 09 February 2012
Check this months hot topics

Using Enum.Parse()

Have you ever written any code along the lines of...

myEnum GetEnumValue(string EnumString)
{
    myEnum enumValue;
    switch (EnumString)
    {
        case "Value1":
            enumValue = myEnum.Value1;
            break;
        case "Value2":
            enumValue = myEnum.Value2;
            break;
        else:
            enumValue = myEnum.ValueNone;
    }
    return enumValue;
}

Embarrassingly, I did. Until I realised that the Enum class had a static method called Parse that can be used as follows...

myEnum GetEnumValue(string EnumString)
{
    return (myEnum)Enum.Parse(typeof(myEnum),EnumString);
}

Easy! Robust! and all that.

kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Dave Howard Skype
Author : Dave Howard
Published : 25 December 2006

I have been involved in IT development for the last 10 years - a lot of it around desktop applications and telecoms.

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views