static void SafeColorToString(EnumLegalColor color) {
if(!Enum.IsDefined(typeof(EnumLegalColor), color))
throw new Exception(string.Format("{0} is not a valid EnumLegalColor", (int) color));
Console.WriteLine(color.ToString());
}
Friday, December 01, 2006
Adding Some Armor to Methods Using Passed Enums
In my previous post I demonstrated the fallibility of methods that take enumerations as parameters. As a defense you can use the Enum.IsDefined() method to check the validity of passed enum values:
Subscribe to:
Post Comments (Atom)
C# Sucks!
JK!! Seriously, though, somewhere around C#-3 we should have inculcated ourselves with the question: "Does 'CAN' == 'SHOULD...
-
I dreamed this whole thing and implemented it on my own over a year ago. Download and install ASP.Net MVC Framework . See steps 7 -9 of prev...
-
Running this code will NOT cause a runtime error if 'b' is not of type 'myclass'. 'a' will simply be assigned null ...
No comments:
Post a Comment