Find current index of a key in a VB collection


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?


Who is Participating?
Richie_SimonettiConnect With a Mentor IT OperationsCommented:
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
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.
Richie_SimonettiIT OperationsCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

sure you can do with an object.
but sometimes a cigar is just a cigar and a string is just a string ;-)
i want to have hashes in vb!!!
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
per recommendation

Community Support Moderator @Experts Exchange
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.