Enum as array index?

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

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
 
Hi Richard,

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

t[ (int)DayOfWeek.Saturday ] = null;


Hope this help,
 
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 said:
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
 
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
 
Back
Top