Check if a record exists in a VB6 collection?

前端 未结 9 1418
失恋的感觉
失恋的感觉 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:02

    Using the error handler to catch cases when the key does not exists in the Collection can make debugging with "break on all errors" option quite annoying. To avoid unwanted errors I quite often create a class which has the stored objects in a Collection and all keys in a Dictionary. Dictionary has exists(key) -function so I can call that before trying to get an object from the collection. You can only store strings in a Dictionary, so a Collection is still needed if you need to store objects.

    0 讨论(0)
  • 2020-12-17 11:12

    While looking for a function like this i designed it as following. This should work with objects and non-objects without assigning new variables.

    Public Function Exists(ByRef Col As Collection, ByVal Key) As Boolean
        On Error GoTo KeyError
        If Not Col(Key) Is Nothing Then
            Exists = True
        Else
            Exists = False
        End If
    
        Exit Function
    KeyError:
        Err.Clear
        Exists = False
    End Function
    
    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题