473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Read Key of a Collection Object Item

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
4 10878
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rick Clift | last post by:
I am trying to add items to a collection object in VB and keep getting the error: "You must implement the Add(System.Object) method on Microsoft.VisualBasic.Collection because it inherits from...
8
by: Ivan Debono | last post by:
Hi, How is it possible to instantiate an new collection in asp serverside?? Thanks, Ivan
5
by: Guojun Zhu | last post by:
I am using VBscript in ASP. I am trying to address one file in a folder object. Below is the relevant code Set MyDirectory=Server.CreateObject("Scripting.FileSystemObject") ' MyFolders is...
3
by: ck | last post by:
Sorry for the cross post. What is wrong with this code?--ado recordset to populate a collection--see reason below Dim myCol2 As New Collection On Error Resume Next Do While Not rs.EOF...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
5
by: aaapaul | last post by:
Hello ! I have some values stored in a xml-file. When starting my application, I read this file and store the values in a collection object. My problem is the error-handling, if the fieldname...
6
by: npaulus | last post by:
Hi, DataSets are usually resource heavy. Instead of having to pass them from my middle tier to my presentation layer I was wondering if anyone has developed a custom collection object that is...
6
by: Hyun-jik Bae | last post by:
Is there any way how to get the item which has the most similar key in key-value collection object such as Dictionary<or SortedDictionary<> although there is no matching key? If there is none, is...
5
by: Rich | last post by:
In VB6 I used to be able to add an object like a textbox and see the dropdown properties of that textbox from the collection object: Dim col as collection set col = new collection col.Add(txt1)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.