By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Read Key of a Collection Object Item

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Kevin L" <no_spam@not_real_email.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...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

Nov 21 '05 #2

P: n/a
Thanks. Much better.

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
"Kevin L" <no_spam@not_real_email.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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


Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
Chris,
Do you mean DictionaryEntry?

No I do not.

Hope this helps
Jay

"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.