By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

Enum as array index?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.