Enum validation question

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
 
J

Jon Skeet [C# MVP]

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).
 
D

David Browne

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
 
H

Hans De Schrijver

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
 

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

Similar Threads

about enum 3
Enum TypeConverter 3
Enum Extentions 7
enum with underlying type 1
Check Flags Enum for Validity 15
enum type 3
finding an Enum using the string name of it 4
Iterate through enum of system.drawing.color 5

Top