Solved

Extract Key from a collection

Posted on 2004-10-15
4
923 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

689 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