all groups > c# > february 2007 >
You're in the

c#

group:

Casting an enum, skips one for no reason?


Re: Casting an enum, skips one for no reason? Bruce Wood
2/2/2007 10:28:58 AM
c#: [quoted text, click to view]

The part I don't understand is why 1 becomes BottomLimit and 2 becomes
TopLimit, since by default, enums start numbering at 0, not 1. You can
make sure that the values are the ones you want by doing this:

public enum LimitType : int
{
BottomLimit = 1,
TopLimit = 2,
Limit = 3
}
Casting an enum, skips one for no reason? PokerMan
2/2/2007 6:24:30 PM
Hi guys,

Maybe someone can explain thisi have this enum:

public enum LimitType : int
{
BottomLimit,
TopLimit,
Limit
}

In another class i read values from my db and cast them to the enum, where
my db id is 1, it will match the enum that is first and so on:

pt = (myClass.LimitType )int.Parse(limitType);

Here is the weird bit, if limitType is a 1, pt becomes BottomLimit. Correct.
if it is a 2 it becomes TopLimit. Correct.
if it is a 3, it becomes the integer 3. Wrong!?

I thought maybe limit is some kind of keyword to the compiler, so i changed
the enum to this:

public enum LimitType : int
{
BottomLimit,
TopLimit,
Limit,
test
}

Ran it again and in debug this happened:

if limitType is a 1, pt becomes BottomLimit. Correct.
if it is a 2 it becomes TopLimit. Correct.
if it is a 3, it becomes test !? Wrong.

Can anyone explain why it seems to be choosing to skip Limit as though it
isn't there? In debug when i check the enum at runtime it does have Limit as
one of the enums and it is in that order. Look forward to the replies.

Re: Casting an enum, skips one for no reason? PokerMan
2/2/2007 6:39:40 PM
lol! you are right it shouldnt work for bottom limit??? I will set the vars
as you said i forgot enums start at 0...thats a tad embarassing, But now i
really want to know why the 1 gave bottom limit.

[quoted text, click to view]

Re: Casting an enum, skips one for no reason? PokerMan
2/2/2007 6:44:00 PM
Specifically assigned values as you said Bruce, i was certain you were
right, thanks for pointing out my stupidity lol. Worked.

I can only assume that i misread it and had been assuming it was reading
them in order. I am going to put it down to human error and 24hrs of working
without a break. Thanks for sorting me out there.

[quoted text, click to view]

Re: Casting an enum, skips one for no reason? Lebesgue
2/4/2007 12:45:17 AM
Reply is inline.

[quoted text, click to view]

Wrong. It becomes TopLimit.

[quoted text, click to view]

Wrong. It becomes Limit.

[quoted text, click to view]

Right. 3 is not defined as value in LimitType.

[quoted text, click to view]

The explanation is that you think that enums are 1-based while they are
0-based.

Your enum actually looks like this:

public enum LimitType : int
{
BottomLimit = 0,
TopLimit = 1,
Limit = 2
}

if you insist on the behavior as you have stated had been occuring so far
(try again please, you must have had something wrong), declare it as
follows:

public enum LimitType : int
{
BottomLimit = 1,
TopLimit = 2,
Limit = 3
}

AddThis Social Bookmark Button