Results 1 to 2 of 2

Thread: Get Index/Key in Collection from other

  1. #1
    Hyperactive Member deathfxu's Avatar
    Join Date
    Mar 09
    Location
    USA
    Posts
    275

    Get Index/Key in Collection from other

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
    4.  
    5. Private Function ItemKey(ByVal Index As Long, Coll As Collection) As String
    6.  
    7.   'optimized get collection sKey by index
    8.   'Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
    9.  
    10.   Dim i     As Long
    11.   Dim Ptr   As Long
    12.   Dim sKey  As String
    13.  
    14.     If Coll Is Nothing Then                             'oops!
    15.         Err.Raise 91                                    'No object
    16.       Else 'NOT COLL...
    17.         Select Case Index
    18.           Case Is < 1, Is > Coll.Count                  'oops!
    19.             Err.Raise 9                                 'Index out of range
    20.           Case Is <= Coll.Count / 2                     'walk items upwards from first
    21.             PokeLong Ptr, ByVal ObjPtr(Coll) + 24       'first Ptr
    22.             For i = 2 To Index
    23.                 PokeLong Ptr, ByVal Ptr + 24            'next Ptr
    24.             Next i
    25.           Case Else                                     'walk items downwards from last
    26.             PokeLong Ptr, ByVal ObjPtr(Coll) + 28       'last Ptr
    27.             For i = Coll.Count - 1 To Index Step -1
    28.                 PokeLong Ptr, ByVal Ptr + 20            'prev Ptr
    29.             Next i
    30.         End Select
    31.         i = StrPtr(sKey)                                'save StrPtr
    32.         PokeLong ByVal VarPtr(sKey), ByVal Ptr + 16     'replace StrPtr by that from collection sKey (which is null if there ain't no sKey)
    33.         ItemKey = sKey                                  'now copy it to function value
    34.         PokeLong ByVal VarPtr(sKey), i                  'and finally restore original StrPtr
    35.     End If
    36.  
    37. End Function
    38.  
    39. Private Function ItemIndex(ByVal Key As String, Coll As Collection, Optional ByVal Compare As VbCompareMethod = vbTextCompare) As Long
    40.  
    41.   'get collection index by key
    42.   'Private Declare Sub PokeLong Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, Optional ByVal Length As Long = 4)
    43.  
    44.   Dim Ptr   As Long
    45.   Dim sKey  As String
    46.   Dim aKey  As Long
    47.  
    48.     If Coll Is Nothing Then                             'oops!
    49.         Err.Raise 91                                    'No object
    50.       Else 'NOT COLL...
    51.         If Coll.Count Then
    52.             aKey = StrPtr(sKey)                         'save StrPtr
    53.             PokeLong Ptr, ByVal ObjPtr(Coll) + 24       'first Ptr
    54.             ItemIndex = 1                               'walk items upwards from first
    55.             Do
    56.                 PokeLong ByVal VarPtr(sKey), ByVal Ptr + 16
    57.                 If StrComp(Key, sKey, Compare) = 0 Then 'equal
    58.                     Exit Do                             'found
    59.                 End If
    60.                 ItemIndex = ItemIndex + 1               'next Index
    61.                 PokeLong Ptr, ByVal Ptr + 24            'next Ptr
    62.             Loop Until Ptr = 0                          'end of chain
    63.             PokeLong ByVal VarPtr(sKey), aKey           'restore original StrPtr
    64.         End If
    65.         If Ptr = 0 Then
    66.             ItemIndex = -1                              'key not found
    67.         End If
    68.     End If
    69.  
    70. End Function

    [code courtesy of PSC]

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 02
    Location
    Finland
    Posts
    6,653

    Re: Get Index/Key in Collection from other

    This is basically the same done more efficiently. I also used more readable method names (but that's a IMO).

    Since I didn't know this post was located in Code Bank, a moderator may merge these two threads if seen necessary.

Posting Permissions

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