By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Enum validation question

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hans De Schrijver <ha**@punctualgraphics.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a

"Hans De Schrijver" <ha**@punctualgraphics.com> wrote in message
news:1h*****************@fe2.texas.rr.com...
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
Nov 16 '05 #3

P: n/a
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" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uj*************@TK2MSFTNGP09.phx.gbl...

"Hans De Schrijver" <ha**@punctualgraphics.com> wrote in message
news:1h*****************@fe2.texas.rr.com...
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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.