473,385 Members | 1,958 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,385 software developers and data experts.

Enum as array index?

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;

// somehow declare strongly typed
MyClass[DayOfWeek] dayObjects = new MyClass
[DayOfWeek.Count];

// then use like ADA, Pascal, or C++
dayObjects[DayOfWeek.Friday].Mood = "Happy";

Is there an easy way to do this?

--Richard
Nov 15 '05 #1
3 16007
Hi Richard,

MyClass[] t = new MyClass[ Enum.GetNames( typeof(DayOfWeek)).Length ];

t[ (int)DayOfWeek.Saturday ] = null;
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Richard" <an*******@discussions.microsoft.com> wrote in message
news:61****************************@phx.gbl...
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;

// somehow declare strongly typed
MyClass[DayOfWeek] dayObjects = new MyClass
[DayOfWeek.Count];

// then use like ADA, Pascal, or C++
dayObjects[DayOfWeek.Friday].Mood = "Happy";

Is there an easy way to do this?

--Richard

Nov 15 '05 #2
Hi Richard,

You can't do what you want quite as straightforwardly as below, but for
common enums like System.DayOfWeek that have all their values in a single
block starting at zero (0-6, in this case) you can do the following:

MyClass[] dayObjects =
new MyClass[Enum.GetValues(typeof(DayOfWeek)).Length];

dayObjects[(int) DayOfWeek.Friday].Mood = "happy";

The things to note are that:

1. Enums in .NET are not just named collections of integer constants as they
are in C and C++. They are actually distinct types. You therefore need to
cast enum values to integers if you want to use them as array indexes.

2. The System.Array type that the C# "MyClass[]" syntax corresponds to is
defined as taking integer index values. If you want to have a collection
type that is strongly typed to accept indices of a particular enum type, you
will need to define it yourself.

3. There isn't a Enum.Count property corresponding to your "DayOfWeek.Count"
syntax below because the notion of the "count" of an enum is not
well-defined -- do you mean the number of named values in the enum? Or
maybe the number of distinct underlying integer values? Or again, maybe you
mean the largest underlying integer value? These can all be different, as
this case shows:

enum MyEnum
{
ValueA = -45,
ValueB = 0,
ValueC = 13,
ValueD = 13
}

There are static functions on the Enum class that allow you to get at the
various features of a particular enum type. These include:

string[] Enum.GetNames(System.Type enumType);
System.Array Enum.GetValues(System.Type enumType);

For System.DayOfWeek you can go and look at the documentation in the .NET
Framework Class Reference and it tells you that the underlying values are
from 0-6, so simply looking at the length of the array returned by
Enum.GetValues is enough. For an arbitrary enum like MyEnum above you would
get back an array of length 3 (containing -45, 0 and 13) and if you didn't
examine those values in more detail bad things would happen when you cast
say, ValueA or ValueC to int and used those values as indices.

Hope that helps,
Nick
"Richard" <an*******@discussions.microsoft.com> wrote in message
news:61****************************@phx.gbl...
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;

// somehow declare strongly typed
MyClass[DayOfWeek] dayObjects = new MyClass
[DayOfWeek.Count];

// then use like ADA, Pascal, or C++
dayObjects[DayOfWeek.Friday].Mood = "Happy";

Is there an easy way to do this?

--Richard


-----------------------------------------------------------------------
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 15 '05 #3
Here's one way:

class DayMood
{
DayFeelings[] days;
public DayMood()
{
days=new
DayFeelings[Enum.GetNames(typeof(DayOfWeek)).Length];
for (int x=0;x<days.Length;x++)
days[x]=new DayFeelings();
}
public DayFeelings this[DayOfWeek day]
{
get
{
return days[(int)day];
}
set
{
days[(int)day]=value;
}
}

public class DayFeelings
{
public string Mood;
}
}
And the calling code would be:
DayMood dm=new DayMood();
dm[DayOfWeek.Friday].Mood="TGIF!";

Austin
On Wed, 28 Jan 2004 11:04:51 -0800, "Richard"
<an*******@discussions.microsoft.com> wrote:
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;

// somehow declare strongly typed
MyClass[DayOfWeek] dayObjects = new MyClass
[DayOfWeek.Count];

// then use like ADA, Pascal, or C++
dayObjects[DayOfWeek.Friday].Mood = "Happy";

Is there an easy way to do this?

--Richard


Nov 15 '05 #4

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 .... ...
2
by: Gregg Teehan | last post by:
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 ;...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.