Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the key of items in collection?

Status
Not open for further replies.

shaoxk66

Technical User
Jan 25, 2007
9
CA
How can I get the key of each item in collection?
here is a sample code.

dim col as new collection
col.add item1, keyitem1
col.add item2, keyitem2
for n = 1 to col.count
item = col.item(n)
? key = col.keys(n)
next n

i can get item by n, but how to get key of item by n?
i tried to use a similar method for scripting.dictionary
to get key, but it does not work. Thanks for helping me out
 
'i think
For Each aKey In Col
Debug.Print aKey & "=" & Col.Item(aKey)
Next
 
that blows to put it mildly :)

Debug.Print aKey works but not the col.Item(aKey)

i was thinking along the lines of a dictionary object but either way the debug.print was a noddy example with strings for values
 
hi mrmovie, i believe col.item(akey) will return an object instead of the key of the object. and it can not be debug.print.
thanks.

(TechnicalUser) 30 Jan 07 10:59
'i think
For Each aKey In Col
Debug.Print aKey & "=" & Col.Item(aKey)
Next

 
Why does that blow? A collection is essentially nothing but keys.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
(I don't believe you can do it with a Collection - it is sometimes a useful tool, but pretty basic.)

Tony, do you know any other one similar to Collection, which can hold a group of user defined class?
i do not like collection either. there are too few methods in this class. but this is the only one i know.
 
If you need to store objects then be able to retrieve them based on some identification, then I would suggest a dictionary.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Reconsider the Scripting.Dictionary object and explore the URL I gave you ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
One method I've used to get around this is, when storing my item in the collection, to instead store a two-element array, of which the first element is the key name:

dim col as new collection
col.add Array(keyitem1, item1), keyitem1
col.add Array(keyitem2, item2), keyitem2
for n = 1 to col.count
item = col.item(n)(1)
key = col.keys(n)(0)
next n

Of course you can switch around which one is first. And with speed in minde you might experiment with a custom TwoArray() function which returns a typed array instead of the variant array using Array().

If I'm remembering correctly, in my tests in the past, this was faster than using the Scripting.Dictionary object, which also had some weird behavior I didn't like that I can't remember now. I don't have the time to do it now but I'd be interested to see performance comparisons of the two methods, including instantiation time for both early-bound and late-bound methods.
 
yeap, i guess i was thinking of a collection to be closely related to a dictionary object, but obviously you can have lots of keys being the same value and hence col.Item(aKey) would lead to problems
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top