Find current index of a key in a VB collection

Posted on 2002-05-24
Last Modified: 2010-08-05

I have a collection

dim bike as new collection

bike.add "red","red_key"
bike.add "gren","green_key"
bike.add "blue","blue_key"
bike.add "purple","purple_key"
bike.add "yellow","yellow_key"


rr = bike("green_key")

will return green


rr = bike(2)

will also return green.

What I would like to do is

somehow find the current index value for a key.

For example: T

he above collection has

the key "purple_key" sitting at index 4
I would like to be able to do something like

indexpurplekey = bike("purple_key").index

How can I get the current index value for a key?


Question by:bgumble
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 7033471
Without itterating through each one, I'm 99% sure you can't. But then there may be a 'backdoor' way of doing this, as there seems to me no reason why this value shouldn't be available.
LVL 16

Expert Comment

ID: 7033752
Function FindInCollection(sKey As String, col As Collection) As Long
Dim o As Long
For o = 1 To col.Count
    If col.Item(o) = col.Item(sKey) Then
        FindInCollection = o
    End If
Next o
End Function
LVL 16

Accepted Solution

Richie_Simonetti earned 50 total points
ID: 7033758
Sorry, FOLLOWING smegghead's comment:

Function FindInCollection(sKey As String, col As Collection) As Long
Dim o As Long
For o = 1 To col.Count
    If col.Item(o) = col.Item(sKey) Then
        FindInCollection = o
        Exit For
    End If
Next o
End Function
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 7034220
LVL 22

Expert Comment

ID: 7034304
To do exactly that: indexpurplekey = bike("purple_key").index

you will need to create a Class Module called, for example, MyBike. This can be NonPublicCreatable. This module contains all properties you want, thus index.

private iIndex as integer
Public property Get Index() As integer
   Index = iIndex
End Property

Friend property Let Index(ByVal mIndex as integer)
   iIndex = mIndex
End Property

Next thing you do is create another class module which will be your collection. Code would look like:

Private mColl as Collection

Private Sub Class_Initialize()
   Set mColl = new Collection
End Sub

Public Sub Add( ...)

End SUb

Public Sub Remove( ...)

End SUb

and all the other functions. This will be your collection which you can also use within a For Each Next loop. Last function you have to add will take care of that and make it a collection.

Public Property Get NewEnum() As IUnknown
   On Error Resume Next
   Set NewEnum = mCollection.[_NewEnum]
End Property

Now go to the ObjectBrowser, select the current project and find the NewEnum() function. Go to the properties -> advanced and set the property id to -4. Also hide the member.

Now you have to add code to the add remove etc functions, you can also add other functions. basically you use mColl to actually add the other items, but underneath the hood you will create an object myBike with all properties set your way which you then add to the collection.

The above is a pretty long story but the best that I know of. You can also do it much simpler by only creating a myBike class which you add to your own collection, but then you have to set the index each time yourself while with a self made collection you can do this programmatically.


Expert Comment

ID: 7034645
sure you can do with an object.
but sometimes a cigar is just a cigar and a string is just a string ;-)

Expert Comment

ID: 7034646
i want to have hashes in vb!!!
LVL 49

Expert Comment

ID: 7818539
Hi bgumble,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Richie_Simonetti's comment(s) as an answer.

bgumble, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7909953
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question