Enum/Array Syntax Error?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 [ WD_INDICE] {0, 0, 0, 0};

I would like to index into the array using the enums like wndCount[DAY].

Thanks for the help.
Peter
 
enum WD_INDICE {DAY,WEEK,MONTH,YEAR};
int[] wndCount = new int [ WD_INDICE] {0, 0, 0, 0};

I would like to index into the array using the enums like wndCount[DAY].

You can't do that. Firstly array indices are integers only. Secondly when
creating an array you need to specify numbers. What is WD_INDICE? It is not
a number.

what you can do is

int[] wndCount = new int [Enum.GetValues(typeof(WD_INDICE)).Length];

Since you want to initialize the array you don't have to specify number of
elements

int[] wndCount = new int [] {0, 0, 0, 0}; // the array will have 4 elements


wndCount[(int)WD_INDICE.YEAR] = 4;

Pay attention on the fact that you can't use enum values directly to index
the array. You have to cast them to int.
 
Hello Peter,

If you are only using that enum for that exact purpose why don't you just create a struct like:
struct WD
{
int Day;
int Week;
int Month;
int Year;
}

// And so you can acces them directly and even have intellisense.
WD wndCount;
wndCount.Day ...

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/
 
Back
Top