Re: Casting an enum, skips one for no reason?
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 2 Feb 2007 10:28:58 -0800
On Feb 2, 10:24 am, "PokerMan" <nos...@xxxxxxxxxxxxxx> wrote:
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.
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
}
.
- Follow-Ups:
- Re: Casting an enum, skips one for no reason?
- From: PokerMan
- Re: Casting an enum, skips one for no reason?
- References:
- Casting an enum, skips one for no reason?
- From: PokerMan
- Casting an enum, skips one for no reason?
- Prev by Date: Re: Problem with C# not calling MC++ destrcutor
- Next by Date: Re: GUI Thread ComInterop
- Previous by thread: Casting an enum, skips one for no reason?
- Next by thread: Re: Casting an enum, skips one for no reason?
- Index(es):
Relevant Pages
|