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

Enums and casting

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.