Check if a record exists in a VB6 collection?

前端 未结 9 1425
失恋的感觉
失恋的感觉 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条回答
  •  萌比男神i
    2020-12-17 11:00

    The statement "error handling will fail if an error handler is already active" is only partly right.

    You can have multiple error handlers within your routine.
    So, one could accommodate the same functionality in only one function.
    Just rewrite your code like this:

    Public Function Exists(col, index) As Boolean
    Dim v As Variant
    
    TryObject:
        On Error GoTo ExistsTryObject
            Set v = col(index)
            Exists = True
            Exit Function
    
    TryNonObject:
        On Error GoTo ExistsTryNonObject
    
            v = col(index)
            Exists = True
            Exit Function
    
    ExistsTryObject:
       ' This will reset your Err Handler
       Resume TryNonObject
    
    ExistsTryNonObject:
            Exists = False
    End Function
    

    However, if you were to only incorporate the code in the TryNonObject section of the routine, this would yield the same information.
    It will succeed for both Objects, and non-objects. It will speed up your code for non-objects, however, since you would only have to perform one single statement to assert that the item exists within the collection.

提交回复
热议问题