Check if a record exists in a VB6 collection?

前端 未结 9 1438
失恋的感觉
失恋的感觉 2020-12-17 10:20

I\'ve inherited a large VB6 app at my current workplace. I\'m kinda learning VB6 on the job and there are a number of problems I\'m having. The major issue at the moment is

9条回答
  •  感动是毒
    2020-12-17 11:16

    see http://www.visualbasic.happycodings.com/Other/code10.html the implementation here has the advantage of also optionally returning the found element, and works with object/native types (according to the comments).

    reproduced here since the link is no longer available:

    Determine if an item exists in a collection

    The following code shows you how to determine if an item exists within a collection.

    Option Explicit
    
    'Purpose     :  Determines if an item already exists in a collection
    'Inputs      :  oCollection         The collection to test for the existance of the item
    '               vIndex              The index of the item.
    '               [vItem]             See Outputs
    'Outputs     :  Returns True if the item already exists in the collection.
    '               [vItem] The value of the item, if it exists, else returns "empty".
    'Notes       :
    'Example     :
    
    Function CollectionItemExists(vIndex As Variant, oCollection As Collection, Optional vItem As Variant) As Boolean
        On Error GoTo ErrNotExist
    
        'Clear output result
        If IsObject(vItem) Then
            Set vItem = Nothing
        Else
            vItem = Empty
        End If
    
        If VarType(vIndex) = vbString Then
            'Test if item exists
            If VarType(oCollection.Item(CStr(vIndex))) = vbObject Then
                'Return an object
                Set vItem = oCollection.Item(CStr(vIndex))
            Else
                'Return an standard variable
                vItem = oCollection.Item(CStr(vIndex))
            End If
        Else
            'Test if item exists
            If VarType(oCollection.Item(Int(vIndex))) = vbObject Then
                'Return an object
                Set vItem = oCollection.Item(Int(vIndex))
            Else
                'Return an standard variable
                vItem = oCollection.Item(Int(vIndex))
            End If
        End If
        'Return success
        CollectionItemExists = True
        Exit Function
    ErrNotExist:
        CollectionItemExists = False
        On Error GoTo 0
    End Function
    
    'Demonstration routine
    Sub Test()
        Dim oColl As New Collection, oValue As Variant
    
        oColl.Add "red1", "KEYA"
        oColl.Add "red2", "KEYB"
        'Return the two items in the collection
        Debug.Print CollectionItemExists("KEYA", oColl, oValue)
        Debug.Print "Returned: " & oValue
        Debug.Print "-----------"
        Debug.Print CollectionItemExists(2, oColl, oValue)
        Debug.Print "Returned: " & oValue
        'Should fail
        Debug.Print CollectionItemExists("KEYC", oColl, oValue)
        Debug.Print "Returned: " & oValue
        Set oColl = Nothing
    End Sub
    
    • See more at: https://web.archive.org/web/20140723190623/http://visualbasic.happycodings.com/other/code10.html#sthash.MlGE42VM.dpuf

提交回复
热议问题