Solved

Extract Key from a collection

Posted on 2004-10-15
4
917 Views
Last Modified: 2008-01-09
Hi,
I have maintained a collection in my Visual basic project using Collection Object. What I did is I inserted addresses of windows procedures of the controls of a form as elements in that collection and inserted handles of those controls as keys in that collection for the corresponding windows procedure addresses. Now, when I pass the key that is the handle of a control, I get the address of that controls. What I want is to extract key of collection also; either using index,
Please give some idea and/or code sample.

Thanks & Regards,
msjammu
0
Comment
Question by:msjammu
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 150 total points
ID: 12327673
Using the standard collection object, there isn't any way to determine the key for an entry.  What you can do however, is create a class to store your data and include the key as one of the values.

' ---------------------------------------------------------
' Class1
' ---------------------------------------------------------
Public key As String
Public someValue As String

' ---------------------------------------------------------
' Form1
' ---------------------------------------------------------
Option Explicit

Private myCollection As Collection

Private Sub Form_Load()
    Set myCollection = New Collection
   
    Dim c As Class1
    Dim i As Integer
   
    For i = 1 To 5
        Set c = New Class1
        c.key = "Item" & i
        c.someValue = "Value" & i
        myCollection.Add c, c.key
    Next i
   
    For Each c In myCollection
        Debug.Print "someValue = " & c.someValue, "key = " & c.key
    Next c
   
    For i = 1 To myCollection.Count
        Set c = myCollection.Item(i)
        Debug.Print "index = " & i, "someValue = " & c.someValue, "key = " & c.key
    Next i
End Sub
0
 
LVL 50

Assisted Solution

by:Dave Brett
Dave Brett earned 100 total points
ID: 12331008
You could try the Dictionary Object instead of a Collection. You can return the Keys

From VBA help

Keys Method
         

Description

Returns an array containing all existing keys in a Dictionary object.

Syntax

object.Keys

The object is always the name of a Dictionary object.

Remarks

The following code illustrates use of the Keys method:

Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.keys              'Get the keys
For i = 0 To d.Count -1 'Iterate the array
    Print a(i)          'Print key
Next
...

Cheers

Dave
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12331022
If you are going to use the Dictionary class I would add a reference to the "Microsoft Scripting Runtime" (Project -->References).  This way, you can declare as below and intellisense will work for you:

    Dim d As Dictionary
    Set d = New Dictionary

~IM
0
 
LVL 6

Author Comment

by:msjammu
ID: 12336220
Thanks you Idle_Mind, brettdj
I will try both the ideas, and choose best that suit me
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 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