If you are using .NET 4.0, then Enum.TryParse<TEnum> is now provided out of the box and support flags enumeration. As given in the documentation, Enum.TryParse<> ‘Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.’
Here’s how to use it:
[Flags]
enum Result { Fail = 0, Pass = 1, Grace = 2 };
static void Main(string[] args)
{
string a = (Result.Pass || Result.Grace).ToString();
Result b;
bool success = Enum.TryParse<Result>(a, out b);
Console.WriteLine("{0} = {1}", success, b);
Console.ReadLine();
}
OUTPUT
Nice post but I think a pipeline between Result.Pass Result.Grace is missed. Am I right?
ReplyDeleteThanks! yes the or symbol was missing..fixed it
ReplyDeleteits trick with Flags Attribute
ReplyDelete