improve my => 'code'
Before I posted a method that converts from one enumeration to another by value... Here's one that does the conversion by string (by the enumeration name)...
public static class EnumHelper
{
public static int ConvertEnumToEnumIntByString<T, U>(U enumArg)
{
string [] arrNames = Enum.GetNames(typeof(T));
int[] arrValues = (int[] ) Enum.GetValues(typeof(T));
for (int counter = 0; counter < arrNames.Length; counter++)
{
if (arrNames[counter] == enumArg.ToString())
{
return arrValues[counter];
}
}
throw new ApplicationException("Enum conversion did not work.");
}
}
private void testNewConversion()
{
Enum1 testInput = Enum1.P1;
Enum2 testOutput = (Enum2) EnumHelper.ConvertEnumToEnumIntByString
< Enum2, Enum1> (testInput);
}
Interested in your comments,
Jonathan