G
Guest
I've been using enum as input parameter to methods in the belief that this
was a safe method of restricting the input parameters, but the example below
proves me wrong.
Is there a way to prevent this?
class Program {
enum ValidColor {
Red = 1,
Blue = 2
}
static void PrintColor(ValidColor color){
switch (color){
case ValidColor.Blue:
case ValidColor.Red:
Console.WriteLine("OK: " + (int)color);
break;
default:
Console.WriteLine("NOT OK!: " + (int)color);
break;
}
}
static void Main(string[] args) {
PrintColor(ValidColor.Blue);
PrintColor(ValidColor.Red);
//make illigal color
ValidColor invalidColor = (ValidColor)22;
PrintColor(invalidColor);
Console.ReadLine();
}
}
ps. I use VS 2005 Beta 2
was a safe method of restricting the input parameters, but the example below
proves me wrong.
Is there a way to prevent this?
class Program {
enum ValidColor {
Red = 1,
Blue = 2
}
static void PrintColor(ValidColor color){
switch (color){
case ValidColor.Blue:
case ValidColor.Red:
Console.WriteLine("OK: " + (int)color);
break;
default:
Console.WriteLine("NOT OK!: " + (int)color);
break;
}
}
static void Main(string[] args) {
PrintColor(ValidColor.Blue);
PrintColor(ValidColor.Red);
//make illigal color
ValidColor invalidColor = (ValidColor)22;
PrintColor(invalidColor);
Console.ReadLine();
}
}
ps. I use VS 2005 Beta 2