Results 1 to 11 of 11

Thread: [Resolved] How To Retrieve Keys from Collection?

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [Resolved] How To Retrieve Keys from Collection?

    If I know the ordinal position of an element in a collection, how can I retrieve the key associated with it?

    I want to get the MyCollection.Items(2).Key ... What to do?

    Is this possible in VB or with the help of some API?
    Last edited by Pradeep1210; May 10th, 2005 at 05:25 AM.

  2. #2
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: How To Retrieve Keys from Collection?

    You can do this if you use the Dictionary Class. It is available if you have the references loaded for FSO.

    Dictionarys work slightly quicker than Collections and they have methods for exporting the items and the keys into one dimentional arrays.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  3. #3
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: How To Retrieve Keys from Collection?

    Quote Originally Posted by Pradeep1210
    If I know the ordinal position of an element in a collection, how can I retrieve the key associated with it?

    I want to get the MyCollection.Items(2).Key ... What to do?

    Is this possible in VB or with the help of some API?

    Default Collection object doesn't have one..
    you can create your own colleciton class for this purpose..

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How To Retrieve Keys from Collection?

    Or you could use something like this:
    VB Code:
    1. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (A As _
    2. Any, b As Any, ByVal C As Long)
    3.  
    4.  
    5. Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Any) As _
    6. Long
    7.  
    8. Public Type CollectionData
    9.    Unk0(0 To 2) As Long
    10.    Unk1 As Long
    11.    ElementCount As Long
    12.    Unk2 As Long
    13.    PtrFirstElement As Long
    14.    PtrLastElement As Long
    15.    Unk3 As Long
    16.    Unk4 As Long
    17.    Unk5 As Long
    18. End Type
    19.  
    20.  
    21. Public Type CollectionElement
    22.    Data As Variant
    23.    Key As Long
    24.    PtrPrev As Long
    25.    PtrNext As Long
    26.    Unk0 As Long
    27.    Unk1 As Long
    28.    Unk2 As Long
    29. End Type
    30.  
    31. Function CollectionGetKeys(ByVal Col As Collection) As String()
    32. Dim asKeys() As String
    33. Dim lIdx As Long
    34. Dim tColl As CollectionData
    35. Dim tColElem As CollectionElement
    36. If Col.Count > 0 Then
    37.     ReDim asKeys(1 To Col.Count)
    38.     MoveMemory tColl, ByVal ObjPtr(Col), LenB(tColl)
    39.     tColElem.PtrNext = tColl.PtrFirstElement
    40.     For lIdx = 1 To tColl.ElementCount
    41.         MoveMemory tColElem, ByVal tColElem.PtrNext, LenB(tColElem)
    42.         asKeys(lIdx) = pvPtr2Str(tColElem.Key)
    43.     Next
    44. End If
    45. CollectionGetKeys = asKeys
    46. End Function
    47.  
    48. Function pvPtr2Str(lpszA As Long) As String
    49. pvPtr2Str = String$(lstrlenW(lpszA), 0)
    50. MoveMemory ByVal StrPtr(pvPtr2Str), ByVal lpszA, lstrlenW(lpszA) * 2
    51. End Function
    ..it can cause problems though. I'm not sure I've done it correctly. But it will return the keys you want.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: How To Retrieve Keys from Collection?

    Quote Originally Posted by chemicalNova
    Or you could use something like this:
    VB Code:
    1. Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (A As _
    2. Any, b As Any, ByVal C As Long)
    3.  
    4.  
    5. Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Any) As _
    6. Long
    7.  
    8. Public Type CollectionData
    9.    Unk0(0 To 2) As Long
    10.    Unk1 As Long
    11.    ElementCount As Long
    12.    Unk2 As Long
    13.    PtrFirstElement As Long
    14.    PtrLastElement As Long
    15.    Unk3 As Long
    16.    Unk4 As Long
    17.    Unk5 As Long
    18. End Type
    19.  
    20.  
    21. Public Type CollectionElement
    22.    Data As Variant
    23.    Key As Long
    24.    PtrPrev As Long
    25.    PtrNext As Long
    26.    Unk0 As Long
    27.    Unk1 As Long
    28.    Unk2 As Long
    29. End Type
    30.  
    31. Function CollectionGetKeys(ByVal Col As Collection) As String()
    32. Dim asKeys() As String
    33. Dim lIdx As Long
    34. Dim tColl As CollectionData
    35. Dim tColElem As CollectionElement
    36. If Col.Count > 0 Then
    37.     ReDim asKeys(1 To Col.Count)
    38.     MoveMemory tColl, ByVal ObjPtr(Col), LenB(tColl)
    39.     tColElem.PtrNext = tColl.PtrFirstElement
    40.     For lIdx = 1 To tColl.ElementCount
    41.         MoveMemory tColElem, ByVal tColElem.PtrNext, LenB(tColElem)
    42.         asKeys(lIdx) = pvPtr2Str(tColElem.Key)
    43.     Next
    44. End If
    45. CollectionGetKeys = asKeys
    46. End Function
    47.  
    48. Function pvPtr2Str(lpszA As Long) As String
    49. pvPtr2Str = String$(lstrlenW(lpszA), 0)
    50. MoveMemory ByVal StrPtr(pvPtr2Str), ByVal lpszA, lstrlenW(lpszA) * 2
    51. End Function
    ..it can cause problems though. I'm not sure I've done it correctly. But it will return the keys you want.

    chem
    Chem.. y would one want to use this way as there are a lot of easier options (like creating custom class or using Dictionary object)

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: How To Retrieve Keys from Collection?

    When I needed it, I didn't want to create a custom class, as I only needed it for a short time to check something in my code. I was just posting this so he could use it, should the need arise.

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How To Retrieve Keys from Collection?

    Yes I should use the Dictionary Object. I think I m using a Collection Object wrongly in this situation.
    But as it will require major changes inmy program, I can't switch over.
    I have just built my own collection class by now (as moinkhan advised)

    Thanks Everyone

    Here is the code for needy people like me:
    This is just the general method I followed. You may wrap it into a class.

    VB Code:
    1. Dim MyCollection As New Collection
    2. Dim sCollectionData(2)
    3.  
    4. ''To add elements
    5. sCollectionData(1) = sKey
    6. sCollectionData(2) = sValue
    7. MyCollection.Add sCollectionData, sKey
    8.  
    9. ''To retrieve it
    10.        'Retrieve by ordinal
    11. sKey = MyCollection(AnyIndex)(1)     ''Key retrieved
    12. sValue = MyCollection(AnyIndex)(2)   ''value retrieved
    13.  
    14.        'retrieve by key
    15. sKey = MyCollection(AnyKey)(1)     ''Key retrieved
    16. sValue = MyCollection(AnyKey)(2)   ''value retrieved
    Last edited by Pradeep1210; May 21st, 2005 at 03:34 AM.

  8. #8
    New Member
    Join Date
    Mar 2011
    Posts
    2

    Re: [Resolved] How To Retrieve Keys from Collection?

    Public Type CollectionElement
    'Data As Variant <--- don't use this
    Data(0 to 15) <---- use this
    Key As Long
    PtrPrev As Long
    PtrNext As Long

    'these can be omitted since you are just reading the linked list
    Unk0 As Long
    Unk1 As Long
    Unk2 As Long
    End Type


    VB initializes the variant with the automation functions for variant management. Once you write over the struct when traversing the collection's linked list, a leak at best or a crash is probable when VB calls VariantClear.

  9. #9

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [Resolved] How To Retrieve Keys from Collection?

    Do you know that you are reviving a 6 year old thread, and that too which is marked "Resolved"?

    And this means bugging everyone who has participated in this thread...

  10. #10
    New Member
    Join Date
    Mar 2011
    Posts
    2

    Re: [Resolved] How To Retrieve Keys from Collection?

    Then quit listening to this (erronously marked as resolved) thread after 6 years already, sir. Do you need help finding the off switch? I could start a how-to thread on that if anyone is interested.

  11. #11

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [Resolved] How To Retrieve Keys from Collection?

    Nothing is marked erroneously. I was the thread starter and after coming up to a good solution for my problem, I marked it resolved.

    And that was 6 years ago. Now there is no problem and no solution. Organization changed, language changed, everything changed. In fact VB6 became obsolete.

    If you are in a similar problem and anything already posted in this thread helps, that's good.
    Otherwise you can start your own new thread for that and many people will reply there. Why spoil a thread unnecessarily like this which contains the solution to a problem in specific situation?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width