473,761 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enums and casting

I started using Enums to make my code more readable. Here is my ENUM:

public enum EntryType : int {
RegularHours = 1,
Lunch = 2,
Vacation = 3,
Sick = 4,
Personal = 5
}

When I am testing against this enum in code I do the following:

if((int)dr["TypeID"] == (int)EntryType. RegularHours) {
// do something
}

Is it always required for me to cast the Enum to int, even though I
already have that set in the enum declaration? otherwise I get a
compile error about not being able to use the '==' operator against int
and EntryType enum

thanks

Sean

Nov 17 '05 #1
4 1937

"DKode" <dk****@gmail.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I started using Enums to make my code more readable. Here is my ENUM:

public enum EntryType : int {
RegularHours = 1,
Lunch = 2,
Vacation = 3,
Sick = 4,
Personal = 5
}

When I am testing against this enum in code I do the following:

if((int)dr["TypeID"] == (int)EntryType. RegularHours) {
// do something
}

Is it always required for me to cast the Enum to int, even though I
already have that set in the enum declaration? otherwise I get a
compile error about not being able to use the '==' operator against int
and EntryType enum

thanks

Sean


Because I'm anal retentive, I tend to do the following:

EntryType typeID = (EntryType) dr["TypeID"];

if (typeID == EntryType.Regul arHours) {
// do something
}

Doesn't answer your question, as I haven't the time to do some testing to
figure it out (can't remember stuff like this off top of my head). Hope it
helps.

:)

Mythran

Nov 17 '05 #2
Well, if you're going to cast dr["TypeID"] to an int, then Yes, you are
going to have to case the enum to an int also. You could try casting the
field value to the enum instead.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"DKode" <dk****@gmail.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
When I am testing against this enum in code I do the following:

if((int)dr["TypeID"] == (int)EntryType. RegularHours) {
// do something
}

Is it always required for me to cast the Enum to int, even though I
already have that set in the enum declaration? otherwise I get a
compile error about not being able to use the '==' operator against int
and EntryType enum

thanks

Sean

Nov 17 '05 #3
I tried casting the field to the Enum and got the same exception.

thought there was an easier way of doing it. oh well

thanks

Nov 17 '05 #4
You have the enums already - why not create the enum from the datareader?
EntryType t = Enum.Parse(type of(EntryType), dr["TypeID"], true);

if(t == EntityType.Regu larHours)
{
stuff...
}

etc.
/Ole
"DKode" <dk****@gmail.c om> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
I started using Enums to make my code more readable. Here is my ENUM:

public enum EntryType : int {
RegularHours = 1,
Lunch = 2,
Vacation = 3,
Sick = 4,
Personal = 5
}

When I am testing against this enum in code I do the following:

if((int)dr["TypeID"] == (int)EntryType. RegularHours) {
// do something
}

Is it always required for me to cast the Enum to int, even though I
already have that set in the enum declaration? otherwise I get a
compile error about not being able to use the '==' operator against int
and EntryType enum

thanks

Sean

Nov 17 '05 #5

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

Similar topics

13
9554
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2080
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
9
1529
by: Simon Elliott | last post by:
I'm porting thousands of lines of legacy C code to C++. One makor issue I've got is that of enums being treated differently in C and C++. There's an automatic cast from int to enum in C but not in C++. The code is full of constructs like this: enum { CARROT=0x01,TURNIP=0x02,PARSNIP=0x04,SPROUT=0x08 } garden_veg; garden_veg = CARROT|PARSNIP; /* Fine in C, but not in C++ */ Do I really have to trawl through all this code and put in many
3
14729
by: Mike Scott | last post by:
What is the best way to index an array by an enum. Pascal has a nice feature in that arrays can be indexed by an enum, for example type ButtonType = ( Left, Middle, Right ) ; var buttons : array of Button ; ....
4
2100
by: Craig | last post by:
Newbie... I'm casting enums back and forth to ints so to use them as an index into a complex two dimensional array. I thought it would help readability. Instead, my code has casts all over the place. All the casting is giving me the jitters. Where did I go wrong? Thanks!
5
1183
by: Nikolay Petrov | last post by:
Let's say I have an Enum Enum Items Item1 = 0 Item2 = 1 Item3 = 2 Item4 = 4 End and I have a method which accepts this Enum as parameter:
11
12552
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course, I can cast a MyEnum instance down to the int / short whatever (since int implements IEquatable<int>), but I don't like doing that, as it feels a bit messy, and I am then propegating the things that know what the base represenation is...
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
37
2202
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
0
9554
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
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9989
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...
0
9811
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8814
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...
0
6640
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();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
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
3
2788
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.