Re: Casting an enum, skips one for no reason?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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
}

.



Relevant Pages

  • Re: Casting an enum, skips one for no reason?
    ... Maybe someone can explain thisi have this enum: ... public enum LimitType: int ... if it is a 2 it becomes TopLimit. ... In debug when i check the enum at runtime it does have Limit ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Casting an enum, skips one for no reason?
    ... Maybe someone can explain thisi have this enum: ... public enum LimitType: int ... Here is the weird bit, if limitType is a 1, pt becomes BottomLimit. ... if it is a 2 it becomes TopLimit. ...
    (microsoft.public.dotnet.languages.csharp)
  • Casting an enum, skips one for no reason?
    ... Maybe someone can explain thisi have this enum: ... public enum LimitType: int ... In debug when i check the enum at runtime it does have Limit as ...
    (microsoft.public.dotnet.languages.csharp)
  • [git patches] net driver updates for .27
    ... static int lance_open (struct net_device *dev) ... enum mac8390_type type); ... unsigned int fifo_count, timeout, reg; ...
    (Linux-Kernel)
  • Re: why still use C?
    ... I was talking about the malloc expression, ... >>the assignment expression. ... >>enum parameter is not an error in C, but in my coding style it's a mistake). ... I took it you meant "int where an enum" is expected, ...
    (comp.lang.c)