473,399 Members | 3,919 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,399 software developers and data experts.

Enum/Array Syntax Error?

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

Nov 16 '05 #1
2 1759
> 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.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #2
Wes
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/
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


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Edward Diener | last post by:
In an assembly I have a __value enum with some enumerated constants, ie. namespace X { public __value enum MyEnum { ValueA, ValueB }; }
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 .... ...
9
by: Xiangliang Meng | last post by:
Hi, all. I see a very strange fragment code today. Uint32 enum { a = 100; b = 200; }; NOTE: Uint32 is defined to be 'unsigned' in other source files.
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 ;...
9
by: Lawrence Oluyede | last post by:
I have a list of strings and i'd like to build up an enum from them... is there a way to do that? Thanks in advance. -- Lawrence "Rhymes" Oluyede http://loluyede.blogspot.com
3
by: Richard | last post by:
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...
6
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I was trying to achieve the following thing: I have a "structure" that basically is a two-dimensional array of numeric values. Both dimensions are handled by enumeration types in C#: ...
13
by: Adam Blair | last post by:
Is it possible to bind a switch statement to an Enum such that a compile-time error is raised if not all values within the Enum are handled in the switch statement? I realise you can use default:...
13
by: Rohit | last post by:
Is there any way by which I can find number of elements in an enum list? Generally I use one last element at the end to find out total number of elements. e.g. typedef enum{ SUN, MON, TUE,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.