Sorry, I missed the definition of OP Enum. Obviously, Enum values ββmust be numeric, so the definition of OP will not work.
It seemed to me that you need to use the char value as an Enum value, for example.
public enum Fruit { Apple = 65, //"A", Banana = 66, // "B", Cherry = 67 //"C" }
According to Convert.ToInt32 ('A') - not sure what to do with case sensitivity. Then take the correct result by casting. I am still playing with an example, glad to hear some suggestions.
OK, sorry for the delay. Here is a bit more about this:
public static class EnumConverter<T> { public static T ToEnum(char charToConvert, out bool success) { try { int intValue = Convert.ToInt32(charToConvert); if (Enum.IsDefined(typeof(T), intValue)) { success = true; return (T)Enum.ToObject(typeof(T), intValue); } } catch (ArgumentException ex) {
Using:
bool success = false; Fruit selected = EnumConverter<Fruit>.ToEnum('A', out success); if (success) {
Robs
source share