Vba-variant: how to handle classes against primitive types

my function gets a collection and the elements can be objects or primitives how can I assign an element to a variant?

Now I look something like this:

Dim vItem As Variant
On Error Resume Next
vItem = oCollection.Item(sKey)
If Err.Number = 91 Then
    Set vItem = oCollection.Item(sKey)
On Error GoTo 0

I think this works, but I wonder if there is a better way to do this.

+3
source share
1 answer

You can use the function varType()to check the type, otherwise, if you are testing certain types, you can use typeof.

        If VarType(oCollection.Item(sKey)) = vbObject Then
           Set vItem = oCollection.Item(sKey)
        Else
            vItem = oCollection.Item(sKey)
        End If
+2
source

All Articles