Enum validation question

  • Thread starter Thread starter Hans De Schrijver
  • Start date Start date
H

Hans De Schrijver

Im puzzled by a data validation behavior I don't understand, related to
enums.

Stripped down scenario:
enum Type {One=1, Two=2, Three=3}
Type var1 = (Type)40;

These two lines of code don't cause a compile error.

In VB, trying to assign a value to a variable of an enum type does cause a
compile error, because the value doesn't exist in the enum.

How can I ensure in C# that this kind of "tweaking" doesn't break my object.
In the validation routines of my class properties I'm already checking for
[object].GetType.Name = "Type" to make sure the passed object is of the
right type before trying to assign it. However, I'd like to know of a good
way to check that the value itself is a valid value in the enum. Any ideas?

-- Hans De Schrijver
 
Hans De Schrijver said:
Im puzzled by a data validation behavior I don't understand, related to
enums.

Stripped down scenario:
enum Type {One=1, Two=2, Three=3}
Type var1 = (Type)40;

These two lines of code don't cause a compile error.

In VB, trying to assign a value to a variable of an enum type does cause a
compile error, because the value doesn't exist in the enum.

How can I ensure in C# that this kind of "tweaking" doesn't break my object.
In the validation routines of my class properties I'm already checking for
[object].GetType.Name = "Type" to make sure the passed object is of the
right type before trying to assign it.

That's a really bad way of doing type checking - use the "as" or "is"
operator.
However, I'd like to know of a good
way to check that the value itself is a valid value in the enum. Any ideas?

Look at Enum.IsDefined(Type, object).
 
Hans De Schrijver said:
Im puzzled by a data validation behavior I don't understand, related to
enums.

Stripped down scenario:
enum Type {One=1, Two=2, Three=3}
Type var1 = (Type)40;

These two lines of code don't cause a compile error.

In VB, trying to assign a value to a variable of an enum type does cause a
compile error, because the value doesn't exist in the enum.

How can I ensure in C# that this kind of "tweaking" doesn't break my object.
In the validation routines of my class properties I'm already checking for
[object].GetType.Name = "Type" to make sure the passed object is of the
right type before trying to assign it. However, I'd like to know of a good
way to check that the value itself is a valid value in the enum. Any ideas?

You need to check the value since enums are really just ints to the
compiler. Most of the stuff that makes them look like classes is done
through reflection. Anyway here's how:

enum foo
{
a = 1,
b,
c
}

if (System.Enum.IsDefined(typeof(foo),2))
{
Console.WriteLine("2 is defined");
}

if (!System.Enum.IsDefined(typeof(foo),4))
{
Console.WriteLine("4 not defined");
}

David
 
Thanks David... knowing the right functions really makes a difference. There
are so many of them that it's going to take a while to get a good handle on
the shear magnitude of the C# .NET library.

-- Hans De Schrijver


David Browne said:
Hans De Schrijver said:
Im puzzled by a data validation behavior I don't understand, related to
enums.

Stripped down scenario:
enum Type {One=1, Two=2, Three=3}
Type var1 = (Type)40;

These two lines of code don't cause a compile error.

In VB, trying to assign a value to a variable of an enum type does cause a
compile error, because the value doesn't exist in the enum.

How can I ensure in C# that this kind of "tweaking" doesn't break my object.
In the validation routines of my class properties I'm already checking for
[object].GetType.Name = "Type" to make sure the passed object is of the
right type before trying to assign it. However, I'd like to know of a good
way to check that the value itself is a valid value in the enum. Any ideas?

You need to check the value since enums are really just ints to the
compiler. Most of the stuff that makes them look like classes is done
through reflection. Anyway here's how:

enum foo
{
a = 1,
b,
c
}

if (System.Enum.IsDefined(typeof(foo),2))
{
Console.WriteLine("2 is defined");
}

if (!System.Enum.IsDefined(typeof(foo),4))
{
Console.WriteLine("4 not defined");
}

David
 
Back
Top