create array of structs, accessed by enum

  • Thread starter Thread starter Zytan
  • Start date Start date
Z

Zytan

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
 
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.
 
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
 
Zytan,

If you are using .NET 2.0 or above, then use the generic Dictionary<K,
V> class in the Sytem.Collections.Generic namespace. If you are using .NET
1.1, then use the Hashtable class in the System.Collections namespace.
 
If you are using .NET 2.0 or above, then use the generic Dictionary<K,
V> class 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
 
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.
 
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
 

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

Back
Top