I want to change the below function that I have written so that it will take the Enum (MyColors in this instance) as argument so that I can use the function on any suitable contiguous Enum. Everything I have tried so far has failed so I’m stuck.
private enum MyColors { Red, Green, Blue }
private String GetNext(String colorName)
{
MyColors colorValue;
String colorNameOut = String.Empty;
if (Enum.TryParse(colorName, out colorValue))
{
MyColors initial = colorValue, next = colorValue;
for (int i = ((Int32)initial) + 1; i < 10; i++)
{
if (Enum.IsDefined(typeof(MyColors), i))
{
next = (MyColors)i;
break;
}
else
{
next = (MyColors)0;
}
}
colorNameOut = next.ToString();
}
return colorNameOut;
}
The following ought to work:
Since the method is now generic it has to be called with a type argument of the enum type, like: