Casting an enum, skips one for no reason?

P

PokerMan

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.
 
B

Bruce Wood

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
}
 
P

PokerMan

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.
 
P

PokerMan

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.
 
L

Lebesgue

Reply is inline.
Maybe someone can explain thisi have this enum:

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

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

Here is the weird bit, if limitType is a 1, pt becomes BottomLimit.
Correct.

Wrong. It becomes TopLimit.
if it is a 2 it becomes TopLimit. Correct.

Wrong. It becomes Limit.
if it is a 3, it becomes the integer 3. Wrong!?

Right. 3 is not defined as value in LimitType.
Can anyone explain why it seems to be choosing to skip Limit as though it
isn't there?

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
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum TypeConverter 3
Enum Extentions 7
enum with underlying type 1
Problem with casting integer values to enum 4
Enum Question 1
Enums and casting 4
Creating dropdown through enum 3
Interface and enum 3

Top