Solved

Extract Key from a collection

Posted on 2004-10-15
4
921 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 86

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 86

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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