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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum TypeConverter 3
enum type 3
enum with underlying type 1
Enum Extentions 7
finding an Enum using the string name of it 4
Question about enum values 10
Merge Info From Two Enums 1
Enum Question 1

Back
Top