Solved

Find current index of a key in a VB collection

Posted on 2002-05-24
9
172 Views
Last Modified: 2010-08-05
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
Comment
Question by:bgumble
9 Comments
 
LVL 10

Expert Comment

by:smegghead
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
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
0
 
LVL 16

Accepted Solution

by:
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
0
 
LVL 6

Expert Comment

by:holli
ID: 7034220
listening...
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:CJ_S
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
   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
 
LVL 6

Expert Comment

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

Expert Comment

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

Expert Comment

by:DanRollins
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
0
 

Expert Comment

by:SpideyMod
ID: 7909953
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now