473,769 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum safety when casting from int

Hi,

Recently we had some code like this cause a failure:

MyEnum myEnum = (MyEnum) (int) dt[row][field];

i.e. reading an int out of the database and casting it into a
type-safe enum.

The thought behind this construct was to enforce type safety and
detect data corruption, which it only partly succeeded in doing.

The bug in our code was that that the wrong field was read from the
datatable, and this allowed an int that was not a defined enum element
to be read instead.

I had thought that this would cause an InvalidCastExce ption, but it
appears that it does not as this test code demonstrates:

class Class1
{
enum MyEnum
{
Zero,
One,
Two,
Three
}

[STAThread]
static void Main(string[] args)
{
MyEnum myEnumA = (MyEnum) 99;
MyEnum myEnumB = (MyEnum) 1;
Console.WriteLi ne(myEnumA.ToSt ring());
Console.WriteLi ne(myEnumB.ToSt ring());
Console.Read();
}
}

This produces the following output:

99
One

The bug was eventually caught by a switch that throws an exception on
the default case, so our code managed to enforce safety eventually :)

It's not really a problem now that we know about it, but it doesn't
seem to be very intuitive as standard behaviour, given that in other
respects enums *are* type-safe, and that Enum.IsDefined can be used to
test whether enum elements are defined or not -- it would be nice to
be able to choose to have that check done automatically, as I think it
would make for safer code.

I'd be interested to hear what others think of this enum behaviour.
Regards,

Matt
Nov 15 '05 #1
3 3008
Matt,

I think it is just fine, because if you enforced something like that,
then you couldn't do the following:

[Flags]
public enum BodyParts
{
Arms,
Legs,
Torso,
Head
}

// Define the body parts that I have:
BodyParts pobjParts = BodyParts.Arms | BodyParts.Legs | BodyParts.Torso |
BodyParts.Head;

// Later on, check to see what body parts the user has.
if ((pobjParts & BodyParts.Head) != 0)
// Tell the user.
Console.WriteLi ne("Nick has a head");

While the example is odd, I think it shows why you can't enforce that.
You will kill all the code that allows for combinations of enumerated values
(which is a concievable operation). In your case, I would have to define
every possible combination for the values in the enumeration. For four
values alone, I would have to define at least twenty-four more enumeration
values just for possible combinations of four elements.

This is why they didn't do it, IMO.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Matt" <kt*******@snea kemail.com> wrote in message
news:55******** *************** ***@posting.goo gle.com...
Hi,

Recently we had some code like this cause a failure:

MyEnum myEnum = (MyEnum) (int) dt[row][field];

i.e. reading an int out of the database and casting it into a
type-safe enum.

The thought behind this construct was to enforce type safety and
detect data corruption, which it only partly succeeded in doing.

The bug in our code was that that the wrong field was read from the
datatable, and this allowed an int that was not a defined enum element
to be read instead.

I had thought that this would cause an InvalidCastExce ption, but it
appears that it does not as this test code demonstrates:

class Class1
{
enum MyEnum
{
Zero,
One,
Two,
Three
}

[STAThread]
static void Main(string[] args)
{
MyEnum myEnumA = (MyEnum) 99;
MyEnum myEnumB = (MyEnum) 1;
Console.WriteLi ne(myEnumA.ToSt ring());
Console.WriteLi ne(myEnumB.ToSt ring());
Console.Read();
}
}

This produces the following output:

99
One

The bug was eventually caught by a switch that throws an exception on
the default case, so our code managed to enforce safety eventually :)

It's not really a problem now that we know about it, but it doesn't
seem to be very intuitive as standard behaviour, given that in other
respects enums *are* type-safe, and that Enum.IsDefined can be used to
test whether enum elements are defined or not -- it would be nice to
be able to choose to have that check done automatically, as I think it
would make for safer code.

I'd be interested to hear what others think of this enum behaviour.
Regards,

Matt

Nov 15 '05 #2
BZZT WRONG.

for bitwise operations to work they need to be powers of 2 for INDIVIDUAL
setting and testing.

This is one of the design flaws in Flags atrtribute i dont like. there is no
way to specify increments in the Attribute.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Matt,

I think it is just fine, because if you enforced something like that,
then you couldn't do the following:

[Flags]
public enum BodyParts
{
Arms,
Legs,
Torso,
Head
}

// Define the body parts that I have:
BodyParts pobjParts = BodyParts.Arms | BodyParts.Legs | BodyParts.Torso |
BodyParts.Head;

// Later on, check to see what body parts the user has.
if ((pobjParts & BodyParts.Head) != 0)
// Tell the user.
Console.WriteLi ne("Nick has a head");

While the example is odd, I think it shows why you can't enforce that.
You will kill all the code that allows for combinations of enumerated values (which is a concievable operation). In your case, I would have to define
every possible combination for the values in the enumeration. For four
values alone, I would have to define at least twenty-four more enumeration
values just for possible combinations of four elements.

This is why they didn't do it, IMO.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Matt" <kt*******@snea kemail.com> wrote in message
news:55******** *************** ***@posting.goo gle.com...
Hi,

Recently we had some code like this cause a failure:

MyEnum myEnum = (MyEnum) (int) dt[row][field];

i.e. reading an int out of the database and casting it into a
type-safe enum.

The thought behind this construct was to enforce type safety and
detect data corruption, which it only partly succeeded in doing.

The bug in our code was that that the wrong field was read from the
datatable, and this allowed an int that was not a defined enum element
to be read instead.

I had thought that this would cause an InvalidCastExce ption, but it
appears that it does not as this test code demonstrates:

class Class1
{
enum MyEnum
{
Zero,
One,
Two,
Three
}

[STAThread]
static void Main(string[] args)
{
MyEnum myEnumA = (MyEnum) 99;
MyEnum myEnumB = (MyEnum) 1;
Console.WriteLi ne(myEnumA.ToSt ring());
Console.WriteLi ne(myEnumB.ToSt ring());
Console.Read();
}
}

This produces the following output:

99
One

The bug was eventually caught by a switch that throws an exception on
the default case, so our code managed to enforce safety eventually :)

It's not really a problem now that we know about it, but it doesn't
seem to be very intuitive as standard behaviour, given that in other
respects enums *are* type-safe, and that Enum.IsDefined can be used to
test whether enum elements are defined or not -- it would be nice to
be able to choose to have that check done automatically, as I think it
would make for safer code.

I'd be interested to hear what others think of this enum behaviour.
Regards,

Matt


Nov 15 '05 #3
Hi Nicholas,

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in message news:<#3******* *******@TK2MSFT NGP09.phx.gbl>. ..
I think it is just fine, because if you enforced something like that,
then you couldn't do the following:
[snip]
While the example is odd, I think it shows why you can't enforce that.
You will kill all the code that allows for combinations of enumerated values
(which is a concievable operation). In your case, I would have to define
every possible combination for the values in the enumeration. For four
values alone, I would have to define at least twenty-four more enumeration
values just for possible combinations of four elements.


I see what you're saying with regard to bitfield enums; as I
originally didn't know that enums could hold non-defined values, I had
simply been storing bitfields in a variable of the underlying type
rather than casting it back to the enum type. I'll cast them back now,
as it will add some more safety.

Anyway what I've done is to implement the following code that our
people can use when they're not using a bitfield enum and want to
ensure that the cast is to a defined element:

public static object EnumTranslate(S ystem.Type enumType, int
enumValue)
{
if ( !Enum.IsDefined (enumType, enumValue) )
{
throw new ArgumentExcepti on( string.Format(" The value '{0}' is
not defined in the enum '{1}'.", enumValue, enumType.ToStri ng()) );
}

return Enum.ToObject(e numType, enumValue);
}

It is called in the following way:

MyEnum myEnumA = (MyEnum) EnumTranslate(t ypeof(MyEnum), myValue);
It is *significantly* slower than a straightforward cast due to the
call to Enum.IsDefined, but it's possible that the IsDefined check
could be compiled only in Debug builds if it turned out to be a
performance problem.

Any suggestions from the group for improvements to this code are
welcome.
Regards,

Matt
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4850
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum EnumName FirstValue = 1 SecondValue = 2 ThirdValue = 3
6
796
by: James Brown | last post by:
Hi, I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING, /*lots more here*/ }; What I am trying to do is _also_ represent ASCII values 0-127 as TOKENs (this is why I started the TOKEN enum off at '1000' so I had plenty of space at the
21
4603
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 {
4
5738
by: marc.gibian | last post by:
I have been trying to improve the quality of my C# and ADO.NET coding. One of the books I've read strongly advises against using string values to address individual values in DataRow objects. This rings true to me after years of avoiding string lookups whenever possible. But, when I attempted to implement this recommendation I appear to run into casting issues. Thus: enum myFields { field1 = 0, field2 = 1
31
3616
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1(); Control c = f; An enum value inherits from int but it doesn't get implicitly converted: HorizontalAlignment h = HorizontalAlignment.Center;
34
11203
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
2
1311
by: SpotNet | last post by:
Hello NewsGroup, All the best for the New Year to you all. Have a question regarding enums and the integer types that can be scoped as. Briefly, if I scope an enum as uint, I cannot use the enum as a uint may be used generally, but must use it as the enum. For example: public enum SomeUInt32Constants: uint
2
3519
by: JB | last post by:
Hi All, I'm pulling my hair over this and could really do with a bit of help. I'm using various different enums as bit fields and I'd like to create a set of generic Functions or Subs to manipulate the bitfields. For instance: <Flags()_ Enum ActionFlags As Byte
11
6409
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently we follow the path below: intValue --enum name --enum value
0
9586
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10043
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.