473,657 Members | 2,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Enums - Feature or deficiency

If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
Dec 9 '07 #1
37 2188
Well, seeing as how what you want to do is so obvious, maybe you'd like to
explain what you want to do so that we, who are obviously deficient in the
area of ESP, can get an idea.
"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:422B1F174E 5748EAAE0F2A995 E0ABD2B@DIMITY. ..
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

Dec 9 '07 #2
On Sun, 9 Dec 2007 16:57:44 +1000, "Ian Semmel"
<an****@rocketc omp.com.auwrote :
>The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
Sadly, no. At least not in the general case. (Although you can use
Length instead of GetLength(0) which is a little bit shorter.)

In your particular case you could simply use (int) en.i3 + 1 because
the default base type of an enumeration is int (Int32) and values
start with zero, incrementing by one with each extra value.

But that requires changing your code whenever you add an enum value.
You could add an extra value, such as "last = i3", and use that
instead for array dimensioning etc. But you'd still have to remember
to update this "last" value when the enumeration changes.
--
http://www.kynosarges.de
Dec 9 '07 #3

"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:422B1F174E 5748EAAE0F2A995 E0ABD2B@DIMITY. ..
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
I would do this (but I'm not a purest):

Enum en
{
i1,
i2,
i3,

enumLength
}

int[] v = new int[enumLength];

Dec 9 '07 #4
Ian Semmel wrote:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

..NET arrays are indexed arrays, not associative arrays. The only way you
can index an array in .NET is through an integer, which is why the
compiler doesn't like you trying to use an enum as its index.

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
Dec 9 '07 #5
Ian Semmel wrote:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
C# is of the C++/Java family - you only specify number
of elements in the array N and array indexes are integers
0..N-1 !

Using types as an array index is a Pascal/Ada family
thing.

Arne
Dec 9 '07 #6

-----Original Message-----
From: BobF [mailto:rN****** *****@charter.n et]
Posted At: Sunday, 9 December 2007 10:31 PM
Posted To: microsoft.publi c.dotnet.langua ges.csharp
Conversation: C# Enums - Feature or deficiency
Subject: Re: C# Enums - Feature or deficiency
"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:422B1F174E 5748EAAE0F2A995 E0ABD2B@DIMITY. ..
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

I would do this (but I'm not a purest):

Enum en
{
i1,
i2,
i3,

enumLength
}

int[] v = new int[enumLength];
Yes, that is the technique I use in C/C++. The reason is that it makes
programs self-resizing as if I add an i4, I don't have to change
anything else.

From other replies in the thread, it seems that C# doesn't allow enums
to be used as array indexes which to me seems like a hole in the
specification as the two are linked in my opinion.

Dec 9 '07 #7

-----Original Message-----
From: Arne Vajhøj [mailto:ar**@vaj hoej.dk]
Posted At: Monday, 10 December 2007 2:21 AM
Posted To: microsoft.publi c.dotnet.langua ges.csharp
Conversation: C# Enums - Feature or deficiency
Subject: Re: C# Enums - Feature or deficiency

Ian Semmel wrote:
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];

C# is of the C++/Java family - you only specify number
of elements in the array N and array indexes are integers
0..N-1 !

Using types as an array index is a Pascal/Ada family
thing.

Arne
Enums have always been the poor cousin in C/C++ and apparently C#.

Dec 9 '07 #8

>
Well, seeing as how what you want to do is so obvious, maybe you'd
like
to
explain what you want to do so that we, who are obviously deficient in
the
area of ESP, can get an idea.
A not uncommon thing I would have thought.

class enAttributes
{
string description;
int somethingelse;
}

enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.
>

"Ian Semmel" <an****@rocketc omp.com.auwrote in message
news:422B1F174E 5748EAAE0F2A995 E0ABD2B@DIMITY. ..
If I have

Enum en
{
i1,
i2,
i3
}

I reckon I should be able to have

int[] v = new int [ en ];

It is obvious what I want to do, but the compiler doesn't like it.

The only way I know to do it is (is there a better way?)

v = new int[ Enum.GetValues( typeof ( en) ).GetLength (0) ];
Dec 9 '07 #9
Ian Semmel <an****@rocketc omp.com.auwrote :

<snip>
enAttributes[] attr = new enAttributes [ en ];

enAttributes a1 = attr [ i1 ];

i1 has a value, I can't see why the compiler can't do the necessary
type-casting.
Because i1 is *not* an integer, it's an enum. If you want to create a
map from enum values to something else, use Dictionary<K,V> .

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 9 '07 #10

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

Similar topics

4
5181
by: Gary Feldman | last post by:
I think I've found a deficiency in the design of urllib related to https. In order to complete an https connection, it appears that URLOpener and hence FancyURLOpener require the key and cert files. Or at least, it's not clear from the description of socket.ssl what it does if they're omitted. However, urlopen has no way to specify such things. Nor should it - for typical uses, a person simply trying to retrieve data from an ssl site...
13
9544
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
3
14725
by: Mike Scott | last post by:
What is the best way to index an array by an enum. Pascal has a nice feature in that arrays can be indexed by an enum, for example type ButtonType = ( Left, Middle, Right ) ; var buttons : array of Button ; ....
37
2205
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
11
373
by: borophyll | last post by:
Hi all, what is the meaning of the second form of an enum declaration enum identifier { enumerator-list } enum identifier { enumerator-list , } as described in the spec. I couldn't see anywhere in the spec that describes what this second form (with the extra comma) means? Regards, B.
8
8783
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.
1
9331
by: CKKwan | last post by:
enum E = {a = 10, b = 10, c = 10}; object Value = 10; E? En = (E?)(int?)Value; Why do I need to caste it to (int?) before (E?) On the other hand, if it is not nullable, it doesn't request to cast to (int)
0
8411
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
8323
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
8739
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
8513
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
7351
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
4173
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...
1
2740
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
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.