473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Enum validation question

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Keith Hill | last post by:
Is there a way to represent a style enum in XML Schema? Say I have a flags enum like so: public enum dayOfWeek { Mon, Tue, Wed, All = Mon | Tue | Wed; }
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
3
by: hufaunder | last post by:
Imagine you have a charting library that can draw lines, bars, floating bars, bands, etc. Lines and bars need only one input. Floating bars and bands need two inputs. There are two approaches: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.