473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.DayOfWeek];
myObjects [DayOfWeek.Monday] = new MyObjectClass();
myObjects [DayOfWeek.Tuesday] = 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(DayOfWeek);
index <= high(DayOfWeek); index++)
{ . . . }

Gregg


Nov 15 '05 #1
2 11576
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.DayOfWeek];
myObjects [DayOfWeek.Monday] = new MyObjectClass();
myObjects [DayOfWeek.Tuesday] = 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(DayOfWeek);
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.Collections. 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.DayOfWeek];
myObjects [DayOfWeek.Monday] = new MyObjectClass();
myObjects [DayOfWeek.Tuesday] = 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(DayOfWeek);
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
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 .... ...
3
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...
2
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}; ...
6
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...
7
by: Harris | last post by:
Dear all, I have the following codes: ====== public enum Enum_Value { Value0 = 0, Value1 = 10,
8
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...
3
by: shapper | last post by:
Hello, I have an enum: Public Enum Color Red Blue Green End Enum
1
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...
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...

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.