I need a helper method to compare a char Enum and a char boxed to an object
c#, casting, enums
Solution
static void Main(string[] args)
{
object val = 'O';
Console.WriteLine(EnumEqual(TransactionStatus.Open, val));
val = 'R';
Console.WriteLine(EnumEqual(DirectionStatus.Left, val));
Console.ReadLine();
}
public static bool EnumEqual(Enum e, object boxedValue)
{
return e.Equals(Enum.ToObject(e.GetType(), (char)boxedValue));
}
public enum TransactionStatus { Open = 'O', Closed = 'C' };
public enum DirectionStatus { Left = 'L', Right = 'R' };
Problem
I have an enum that looks as follows: ``` public enum TransactionStatus { Open = 'O', Closed = 'C'}; ``` and I'm pulling data from the database with a single character indicating - you guessed it - whether 'O' the transaction is open or 'C' the transaction is closed. now because the data comes out of the database as an object I am having a heck of a time writing comparison code. The best I can do is to write: ``` protected bool CharEnumEqualsCharObj(TransactionStatus enum_status, object obj_status) { return ((char)enum_status).ToString() == obj_status.ToString(); } ``` However, this is not the only character enum that I have to deal with, I have 5 or 6 and writting the same method for them is annoying to say the least. Supposedly all enums inherit from System.Enum but if I try to set that as the input type I get compilation errors. This is also in .NET 1.1 so generics are out of the question. I've been struggling with this for a while. Does anyone have a better way of writing this method? Also, can anyone clarify the whole enums inherit from System.Enum but are not polymorphic thing?