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