473,405 Members | 2,415 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,405 software developers and data experts.

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 1913

"DKode" <dk****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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.RegularHours) {
// 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.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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(typeof(EntryType), dr["TypeID"], true);

if(t == EntityType.RegularHours)
{
stuff...
}

etc.
/Ole
"DKode" <dk****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.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
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...
2
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...
9
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...
3
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 :...
4
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...
5
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
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,...
2
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...
37
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.