473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting an enum, skips one for no reason?

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.LimitT ype )int.Parse(limi tType);

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.
Feb 2 '07 #1
4 1287
On Feb 2, 10:24 am, "PokerMan" <nos...@pokerca t.co.ukwrote:
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.LimitT ype )int.Parse(limi tType);

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
}

Feb 2 '07 #2
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.

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
On Feb 2, 10:24 am, "PokerMan" <nos...@pokerca t.co.ukwrote:
>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.LimitT ype )int.Parse(limi tType);

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
}

Feb 2 '07 #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.

"PokerMan" <no****@pokerca t.co.ukwrote in message
news:%2******** ********@TK2MSF TNGP03.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.LimitT ype )int.Parse(limi tType);

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.

Feb 2 '07 #4
Reply is inline.
Maybe someone can explain thisi have this enum:

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

pt = (myClass.LimitT ype )int.Parse(limi tType);

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
}
Feb 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1420
by: Jean Stax | last post by:
Hi ! Jeffrey Richter gives the following example in his book: struct Point { Int32 x, y; } class AAA{ .... public virtual void Add(Object value);
3
3000
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought behind this construct was to enforce type safety and
18
11340
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
1
281
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum field UserType) and for each type of user a inherited class like SessionMasterUser and SessionNormalUser.
0
1712
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. I'm able to use this DLL without any problems within my test windows app, but not within my windows service (that's when I receive the casting exception). Here's the code (sorry it's long): VB Function within windows app and windows service:
8
8609
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have to exist in another class? Thanks for any help!
3
3646
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
2
2479
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
4
1981
by: mitdej | last post by:
Hi there, I have an several enum types that starts from a nunmber other than 0. For example: public enum InternalStatus { Pending = 1, Ported = 2, Suspended = 3 } I put this values in a int column of a MSSQL table.
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.