At the moment I am parsing strings to enumerations using this code:

if (Enum.IsDefined(typeof(Color), input))
{
    colour = (Color)Enum.Parse(typeof(Color), input);
}

which has worked fine so-far, but I would like to parse lower case/mixed case, for example someone might input Red, but the enumeration is red or RED so I need a method where I can pass a type like this:

public Enum ParseEnum(Type enumType, String compare)
{
    if (enumType.IsEnum)
    {
        int i = 0;
        foreach (string str in Enum.GetNames(enumType))
        {
            if (String.Equals(str, compare, StringComparison.OrdinalIgnoreCase) )
            {
                return (Enum)i;
            }
            i++;
        }
    }
}

The problem (at the moment) is that the cast of integer to enum comes up with the error "error CS0030: Cannot convert type 'int' to 'System.Enum'"

Can anyone help? Thanks

Joe

Just realised a String.ToUpper() method will work if all my Enums are upper case

following code will help you convert the string in what ever case you want

String str;
str.ToUpper();// will convert the string to upper case
str.ToLower();//will convert the string to lower case

oopse guess U have already got ur answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.