473,770 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enums ... what am I doing wrong?

Newbie...

I'm casting enums back and forth to ints so to use them as an index into a
complex two dimensional array. I thought it would help readability.
Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!
Nov 16 '05 #1
4 2100
Why are you casting them in the first place? :)

public enum ConnectionStatu s : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting, Disconnected
}
ShaneB
"Craig" <sp******@hotma il.com> wrote in message
news:Xv******** ***********@twi ster.socal.rr.c om...
Newbie...

I'm casting enums back and forth to ints so to use them as an index into a
complex two dimensional array. I thought it would help readability.
Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!

Nov 16 '05 #2
ShaneB,

There is no implicit cast for enums to their underlaying type.
public enum ConnectionStatu s : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting, Disconnected
}
In fact, despite of the that syntax c# uses, ConnectionStatu s doesn't
inherit from *int*

Thus, in the case of using enum values for indexing arrays casting is
necessary.
--
Stoitcho Goutsev (100) [C# MVP]
"ShaneB" <st********@yah oo.com> wrote in message
news:ev******** ******@TK2MSFTN GP11.phx.gbl... Why are you casting them in the first place? :)

public enum ConnectionStatu s : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting, Disconnected
}
ShaneB
"Craig" <sp******@hotma il.com> wrote in message
news:Xv******** ***********@twi ster.socal.rr.c om...
Newbie...

I'm casting enums back and forth to ints so to use them as an index into
a complex two dimensional array. I thought it would help readability.
Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!


Nov 16 '05 #3
Actually, that post should have been canceled. Two seconds after I hit
send, I realized my mistake :)

ShaneB

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:uf******** ******@tk2msftn gp13.phx.gbl...
ShaneB,

There is no implicit cast for enums to their underlaying type.
public enum ConnectionStatu s : int
{
Resolving, Resolved, Connecting, Connected, Disconnecting,
Disconnected
}


In fact, despite of the that syntax c# uses, ConnectionStatu s doesn't
inherit from *int*

Thus, in the case of using enum values for indexing arrays casting is
necessary.


Nov 16 '05 #4
Craig wrote:
Newbie...

I'm casting enums back and forth to ints so to use them as an index
into a complex two dimensional array. I thought it would help
readability. Instead, my code has casts all over the place.

All the casting is giving me the jitters. Where did I go wrong?

Thanks!


How about using constants instead of enum values?
* same readability of enum (words instead of "magic numbers")
* they really are int's

public const int WHATEVER = 3;
myval = store[WHATEVER];

Hans Kesting
Nov 16 '05 #5

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

Similar topics

27
2742
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template <typename Enum> class smart_enum { public: typedef Enum enum_type;
9
1767
by: Jarmo Muukka | last post by:
Hello all! I have used C++ for many years and recently I have used C#. Now I am coding in C++ and I have a problem now. In C# you can do enums like this: enum Lane { Left,
37
2231
by: mdh | last post by:
In one of the answers to a K&R exercise, the first couple of lines are: enum loop { NO, YES}; enum loop okloop=YES; I get the first line, but not the second. Sorry about the LOL question. Thanks in advance
13
5013
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to declare that an enum exists. i.e. It is within the 'spirit' of an interface to state that it exists. The implementing class will provide the members. e.g. I have a code library that performs a function. It exposes two interfaces which allow for...
3
1561
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
2
4130
by: olympus_mons | last post by:
Hi, I'm just discovering the power of xsd.exe, so maybe I'm doing something wrong. schema files describing requests and responses. So there is an extra xsd file for each response and each request as well as some base schemas, i.e. defining a data type for a result element. Among others this result element contains a simple enumeration type "ResultState" which can either be "OK" or "FAILED".
2
2052
by: Zytan | last post by:
I know you can use enums as a bit field with , but, when I use an enum without , I can still combine one or more of them. There's no error in doing so. I thought enums were strongly typed, such that you need an explicit cast from an int to get a wrong value into an enum. class Program { enum RGB0 {
37
2207
by: Ian Semmel | last post by:
If I have Enum en { i1, i2, i3 } I reckon I should be able to have
8
8793
by: Daniel Gutson | last post by:
Hi, I just wanted to share another library for doing type-safe bitwise operations in C++: http://bitwise-enum.googlecode.com I found it useful, so hopefully it'll be for somebody else as well. BRgds, Daniel.
0
9591
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
9425
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
10228
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
10057
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
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
3575
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.