473,699 Members | 2,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum as array index

Suggestions on best practive for array declare / subscript
using an enum - this is easy in Delphi / Object Pascal.

I want

private MyObjectClass [] myObjects;

myObjects = new MyObjectClass [DateTime.DayOfW eek];
myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;

but C# says array subscript must be int type

How best to do this.
1) what is syntax for cardinality of DayOfWeek enum?
2) what is 1st and last of enumeration?
e.g.
for (DayOfWeek index=low(DayOf Week);
index <= high(DayOfWeek) ; index++)
{ . . . }

Gregg


Nov 15 '05 #1
2 11592
Gregg,

There is nothing in the framework for this, so you will have to do it
yourself.

You can call the static GetValues method on the Enum type to get the
values in the enumeration. You can then sort that array. Once you have
that, you can get the first and last values in the enumeration.

Hope this helps.

i
"Gregg Teehan" <gt*****@nummi. com> wrote in message
news:03******** *************** *****@phx.gbl.. .
Suggestions on best practive for array declare / subscript
using an enum - this is easy in Delphi / Object Pascal.

I want

private MyObjectClass [] myObjects;

myObjects = new MyObjectClass [DateTime.DayOfW eek];
myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;

but C# says array subscript must be int type

How best to do this.
1) what is syntax for cardinality of DayOfWeek enum?
2) what is 1st and last of enumeration?
e.g.
for (DayOfWeek index=low(DayOf Week);
index <= high(DayOfWeek) ; index++)
{ . . . }

Gregg


Nov 15 '05 #2
Gregg,
In addition to Nicholas's comments.

When I want a collection keyed by an Enum, I consider using a Hashtable
instead of an Array.

This allows me to use the Enum itself for the key.

Of course the Hashtable will be more overhead then an Array (or ArrayList),
which I take into consideration.

Truthfully I normally create type safe collections with either
CollectionBase or DictionaryBase in System.Collecti ons. In the case of an
enum index/key, I define the parameter to the indexer, as the enum.

I think when we get to .NET 2.0 (Whidbey, aka VS.NET 2004) we will have a
couple of better options available in terms of Generics! :-))

Hope this helps
Jay

"Gregg Teehan" <gt*****@nummi. com> wrote in message
news:03******** *************** *****@phx.gbl.. .
Suggestions on best practive for array declare / subscript
using an enum - this is easy in Delphi / Object Pascal.

I want

private MyObjectClass [] myObjects;

myObjects = new MyObjectClass [DateTime.DayOfW eek];
myObjects [DayOfWeek.Monda y] = new MyObjectClass() ;
myObjects [DayOfWeek.Tuesd ay] = new MyObjectClass() ;

but C# says array subscript must be int type

How best to do this.
1) what is syntax for cardinality of DayOfWeek enum?
2) what is 1st and last of enumeration?
e.g.
for (DayOfWeek index=low(DayOf Week);
index <= high(DayOfWeek) ; index++)
{ . . . }

Gregg


Nov 15 '05 #3

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

Similar topics

10
9086
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... const enum ENUM_LIST {A = 0, B, C, D, E, ENUM_LIST_MAX}; RWCString MESSAGES; <--------------------- MESSAGES = "MESSAGE_A";
3
16069
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like the array declaration to be strongly typed to the enum and I'd like to use the enum to access entries in the array. I'd like to write code something like this: #using System;
2
1766
by: Peter | last post by:
Hello, Thanks for reviewing my question. I would like to know why I am getting a syntax error on the following: enum WD_INDICE {DAY,WEEK,MONTH,YEAR}; int wndCount = new int {0, 0, 0, 0}; I would like to index into the array using the enums like wndCount.
6
4248
by: Brett Romero | last post by:
I'd like to create one enum that holds webpage name values and corresponding URL values. For example: enum webPageFileName {index = "index.aspx"} enum webPageName {index = "index"} enum webPageNameTitle
7
9829
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
2273
by: Joe Rattz | last post by:
Ok, I can't believe what I am seeing. I am sure I do this other places with no problems, but I sure can't here. I have some code that is indexing into the ItemArray in a DataSet's DataRow. I basically want to change the value of the data stored at a particular index in the ItemArray. Ideally, I would like for my code to look like this (pay attention to the index of ItemArray): row.ItemArray = itemArray.ToString().Substring(3);
3
5230
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
1
1807
by: toton | last post by:
Hi, I have an enum as enum DitectionType{ X,Y,XD,YD } ; Now I want to wrap it with a class which returns orthogonal direction for it. i.e X -Y , Y-X, XD-YD, YD->XD mapping should be there. I can think of an static array which stores this mapping, and returns the value based on index, rather than doing some if else. Can anybody hint a good way to do it ( the primitive way is an global
7
7961
by: Kevin | last post by:
I'm creating a template to support state machines. In doing so, I need to pass an enumeration for the number of transitions and a non type parameter for the range of the enum (to allow me to allocate an array of pointers big enough to hold one entry for each transition). My template declaration looks roughly like this: template <class TRANSITION, int NUMTRANSITIONS> class StateMachine { ***SNIP*** void RegisterTransition(TRANSITION...
0
8615
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
9174
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...
1
8914
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
8883
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
7750
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
5874
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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
3
2009
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.