Read Key of a Collection Object Item

  • Thread starter Thread starter Kevin L
  • Start date Start date
K

Kevin L

I have a collection.
I am looping through the collection using a FOR EACH.

Is there a way to access the Key from the current item?


This code obviously does not work, but I am providing it to illustrate the
value that I am trying to get.


Dim MyCollection as New Collection
Dim objItem as Object

FOR EACH objItem in MyCollection
msgbox(objitem.KEY)
NEXT
 
Kevin,
Instead of using VB.Collection consider using System.Collections.HashTable.

Then when you use For Each you get back DictionaryEntry values, which
contain the value & the key.
Dim MyCollection as New HashTable Dim entry As DictionaryEntry
Dim objItem as Object

FOR EACH entry in MyCollection objItem = entry.Value
msgbox(entry.KEY)
NEXT

I normally use the collections in System.Collections &
System.Collections.Specialized as they are more specific to the task at
hand, where as VB.Collection is a little too generalized. Mostly I derive
type safe classes from either DictionaryBase or CollectionBase.

Hope this helps
Jay
 
Thanks. Much better.





Jay B. Harlow said:
Kevin,
Instead of using VB.Collection consider using
System.Collections.HashTable.

Then when you use For Each you get back DictionaryEntry values, which
contain the value & the key.


I normally use the collections in System.Collections &
System.Collections.Specialized as they are more specific to the task at
hand, where as VB.Collection is a little too generalized. Mostly I derive
type safe classes from either DictionaryBase or CollectionBase.

Hope this helps
Jay
 
Jay, do you know off the top of your head if .Net 2.0 has a generic
HashTable that does not return a dictionaryObject, but returns the
correct object?

Just wondering.

Chris
 
Chris,
Do you mean DictionaryEntry?

No I do not.

Hope this helps
Jay
 

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