473,387 Members | 1,374 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,387 software developers and data experts.

create array of structs, accessed by enum

I have an enum. I want a struct of data associated with each one.
Then, I could use the enum to access the data as needed. In other
words, I want the enum to represent more than just a unique integer.
I want it to represent a struct.

Is there a way to do this nicely?

Searching seems to say "no."

(Enum.GetValues(myenum).Length is not a constant, apparantly, which is
annoying, since I can't use it to *at least* size the array, unless I
want to create it dynamically, which would solve a portion of this
problem, but not all.)

Zytan

Mar 28 '07 #1
6 2209
Zytan,

You should use a dictionary instead. Even if you could size the array
properly, you would have to cast the enumeration value to an int every time
you tried to get an element from the array.

The dictionary would make it much easier to use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
>I have an enum. I want a struct of data associated with each one.
Then, I could use the enum to access the data as needed. In other
words, I want the enum to represent more than just a unique integer.
I want it to represent a struct.

Is there a way to do this nicely?

Searching seems to say "no."

(Enum.GetValues(myenum).Length is not a constant, apparantly, which is
annoying, since I can't use it to *at least* size the array, unless I
want to create it dynamically, which would solve a portion of this
problem, but not all.)

Zytan

Mar 28 '07 #2
You should use a dictionary instead. Even if you could size the array
properly, you would have to cast the enumeration value to an int every time
you tried to get an element from the array.

The dictionary would make it much easier to use.
Ok, thanks, I'll reseach dictionaries and see what they are about.

Zytan

Mar 28 '07 #3
Zytan,

If you are using .NET 2.0 or above, then use the generic Dictionary<K,
Vclass in the Sytem.Collections.Generic namespace. If you are using .NET
1.1, then use the Hashtable class in the System.Collections namespace.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
> You should use a dictionary instead. Even if you could size the
array
properly, you would have to cast the enumeration value to an int every
time
you tried to get an element from the array.

The dictionary would make it much easier to use.

Ok, thanks, I'll reseach dictionaries and see what they are about.

Zytan

Mar 28 '07 #4
If you are using .NET 2.0 or above, then use the generic Dictionary<K,
Vclass in the Sytem.Collections.Generic namespace. If you are using .NET
1.1, then use the Hashtable class in the System.Collections namespace.
Thanks, I'm using 2.0. Sounds like Dictionary is a HashTable, except
its strongly typed, and it throws an exception on illegal access.
Dictionary must be a replacement for HashTable.

This should be fine.

Is there any way to initialize a Dictionary in the declaration? I
don't think so, but it'd be beneficial, since it will essentially be a
constant in my program.

Thanks for your help,

Zytan

Mar 28 '07 #5
Zytan,

Unfortunately, no, there is no way to populate the Dictionary in the
declaration. C# 3.0 will have a mechanism to do that, but that's a ways
off.

And you are right, the Dictionary is really just a strongly-typed hash
table.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11*********************@n76g2000hsh.googlegro ups.com...
> If you are using .NET 2.0 or above, then use the generic
Dictionary<K,
Vclass in the Sytem.Collections.Generic namespace. If you are using
.NET
1.1, then use the Hashtable class in the System.Collections namespace.

Thanks, I'm using 2.0. Sounds like Dictionary is a HashTable, except
its strongly typed, and it throws an exception on illegal access.
Dictionary must be a replacement for HashTable.

This should be fine.

Is there any way to initialize a Dictionary in the declaration? I
don't think so, but it'd be beneficial, since it will essentially be a
constant in my program.

Thanks for your help,

Zytan

Mar 28 '07 #6
Unfortunately, no, there is no way to populate the Dictionary in the
declaration. C# 3.0 will have a mechanism to do that, but that's a ways
off.

And you are right, the Dictionary is really just a strongly-typed hash
table.
Ok, great, thanks, Nicholas, your help has been wonderful!

Zytan

Mar 28 '07 #7

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

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
36
by: Eric Laberge | last post by:
Hi! I'm working on automatically generated code, and need to assign arrays. memcpy is an obvious solution, but it becomes complicated to use in the context I'm working on, ie.: I could use it...
15
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
11
by: Pedro Pinto | last post by:
Hi there! I'm having a problem in my program. i've created this structure: typedef struct tabela tabela; struct tabela { char nome; int nColunas; int nLinhas; char nomeColunas;
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
16
by: andreyvul | last post by:
If I try compiling this in gcc, it says: "error: request for member `baz' in something not a structure or union". Any workarounds or tips on how to make a structure such that it behaves like an...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
3
by: charlesh3 | last post by:
I'm an engineer with a C background that is trying to do my first c# project. I created a struct: public struct MyStruct { public static UInt32 bUsed; public...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.