468,119 Members | 1,804 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,119 developers. It's quick & easy.

Dictionary collection

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?
Jul 22 '05 #1
6 1424
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];
Jul 22 '05 #2
Forget it. Doesn't work.

Jul 22 '05 #3
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 :)

"Adam" wrote:
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];
.
.
.
}

Like I said, this is off the top of my head and I haven't tried it. I
apologize in advance if I have misled you.

Regards,

Adam

Jul 22 '05 #4
Dick,

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

Brian

Dick wrote:
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?


Jul 22 '05 #5

Hi there,

Please see System.Collections.Specialized.NameValueCollection

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

Good luck!

--
Juan Romero
-----------------------------------------
The successful person has the habit of doing the things failures don't like
to do.
E.M. Gray
"Dick" wrote:
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?

Jul 22 '05 #6
Madestro <me_no_like_spam_juanDOTromero@bowneDOTcom> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by What-a-Tool | last post: by
7 posts views Thread by Andrew Robinson | last post: by
2 posts views Thread by Stephen Costanzo | last post: by
2 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.