• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 180
  • Last Modified:

Find current index of a key in a VB collection

Hi,

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"

so


rr = bike("green_key")

will return green


and

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?


Thanks



0
bgumble
Asked:
bgumble
1 Solution
 
smeggheadCommented:
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.
0
 
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
0
 
Richie_SimonettiIT 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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
holliCommented:
listening...
0
 
CJ_SCommented:
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
   Err.Clear
   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.

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

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now