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
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