c#:
[quoted text, click to view] On Feb 2, 10:24 am, "PokerMan" <nos...@pokercat.co.uk> 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 }
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.
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] "Bruce Wood" <brucewood@canada.com> wrote in message news:1170440938.077993.184860@m58g2000cwm.googlegroups.com... > On Feb 2, 10:24 am, "PokerMan" <nos...@pokercat.co.uk> 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 > } >
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] "PokerMan" <nospam@pokercat.co.uk> wrote in message news:%23t4chdvRHHA.4060@TK2MSFTNGP03.phx.gbl... > 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. >
Reply is inline. [quoted text, click to view] > 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. [quoted text, click to view] > if it is a 2 it becomes TopLimit. Correct.
Wrong. It becomes Limit. [quoted text, click to view] > if it is a 3, it becomes the integer 3. Wrong!?
Right. 3 is not defined as value in LimitType. [quoted text, click to view] > 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 }
Don't see what you're looking for? Try a search.
|