Enums I thought they were safe to use

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
Hi,

Using Enums does not gurantee the value passed falls within the enum range
defined. You need to validate this youself.

Eg. for validation:
if(color < ValidColor.Red || color > ValidColor.Blue )
{
throw new ArgumentException("Invalid Color");
}
 
A better test is

if(Enum.isDefined(typeof(ValidColor), color))
throw new ArgumentException("Invalid Color");

which works however many entries are in the enum

Rakesh Rajan said:
Hi,

Using Enums does not gurantee the value passed falls within the enum range
defined. You need to validate this youself.

Eg. for validation:
if(color < ValidColor.Red || color > ValidColor.Blue )
{
throw new ArgumentException("Invalid Color");
}

--
HTH,
Rakesh Rajan
MVP, MCSD
http://www.msmvps.com/rakeshrajan/



Rasmus said:
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
 
Ok, i've just misunderstood how Enums worked then.

Bu what if you have a realy huge Enum, and don't want to do validation. What
should you use instead?
 
enum Colors : int
{
ColorRed = 0,
//..,
//..,
ColorInvalidColor
}


if(value >= Colors.ColorRed && value < Colors.ColorInvalidColor)
{}


Pretty much exactly what the first responder did, but reads a little nicer
(IMO)
Might be dumb, I dunno.. that's what I do.
 
This will help, but as i asked: what can i use if i don't want to do
validation?

/Rasmus
 
Rasmus said:
This will help, but as i asked: what can i use if i don't want to do
validation?

If you want a data type that'll always be valid, you'll have to create
your own type in the form of a struct or a class. But if that data type is
being constructed with the help of other, integral, types, you'll have to
do validation again, though in a different place - remember that within
the constraints posed by your application model and the methods you write
for it, you're the only one who can judge the validity of a given set of
information.



Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top