# Tuesday, November 30, 2004
« Tip of the Day: Simulating the CHAR func... | Main | WinCV revisited... »

Here's another tip which is a wrapper to retrieve the value from an enum by it's text and also to get the text of an enum based on a value.

public static string RetrieveEnumTextByValue (Type enumType, Object value)
{
    if (enumType.IsEnum)
    {
        if (System.Enum.GetName(enumType,value) != null)
            return System.Enum.GetName(enumType, value);
    }
    return "";
}
   
public static int RetrieveEnumValueByText(Type enumType, string text)
{
    if (enumType.IsEnum)
    {
        if (System.Enum.Parse(enumType,text) != null)
            return Convert.ToInt32(System.Enum.Parse(enumType, text));
    }
    return -1;
}

I'm a little lazy tonight, but maybe I will come in later and post usage. I'll make a better effort to do that in the future, but I have to be up in a few hours, and being tired tomorrow just won't do!

-- Daryl

Comments are closed.