Dictionary collection

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

Guest

Is there anything that combines the features of a dictionary and a collection
such that items can be retrieved by either their key or their index? I think
this is how vb.6 collections worked?
 
I haven't tried this, but I think that for any of the dictionary-type
collections (Hashtable, HybridDictionary, ListDictionary, et al.) you
could access the values by index by using the Values property, which
should be indexable.

Hastable foo = new Hastable();

// Populate hashtable
..
..
..

// Index through items
for (int index = 0; index < foo.Count; ++index) {
object bar = foo.Values[index];
 
Sorry but MyBase.Dictionary.Values returns an ICollection which, as far as I
can, isn't indexable. As you say, you didn't try it, so appologies accepted :)
 
Dick,

You can reference Microsoft.VisualBasic.dll and use the Collection
class.

Brian
 
Hi there,

Please see System.Collections.Specialized.NameValueCollection

The Specialized namespace contains collections that do what you are looking
for.

Good luck!
 
Madestro said:
Please see System.Collections.Specialized.NameValueCollection

The Specialized namespace contains collections that do what you are looking
for.

It's not quite the same:

1) It only deals with strings
2) It adds multiple values for the same key, comma-separating them

I don't see anything in System.Collections.Specialized which solves the
OP's problem, unless he happens to be looking for strings.

Personally I'd write a class which aggregates a list and a dictionary,
and provide the appropriate access methods. (Care is needed in terms of
what it means to replace the entry at a particular index, etc - it may
be simpler to make it an "add only" type of collection.)
 
Back
Top