Search code examples
vb6

Is Nothing comparison gives type mismatch


I am trying to check if the 'Listivew.Tag property is nothing'.

I used to do the 'Is Nothing' check universally for all scenarios as first check to avoid errors

Can someone explain how to do it in VB 6?

 If Not .lvwLocation.Tag Is Nothing Then
    'COMPANY
    str = str & IIf(Len(.lvwLocation.Tag) > 0, " and u.location_id in " & .lvwLocation.Tag, "")
End If

Gives error 'type-mismatch'


Solution

  • Nothing is a valid value for Object variables, and Is is the way to compare object pointers.

    But a VB6 control's Tag property is a String, and VB6's String type is not an Object; it's a primitive type. That means a String variable can't be assigned Nothing -- its emptiest possible value is the empty string. (And an Object variable can't be assigned a String value.) For strings just use the same equality/inequality/comparision operators that you use for other primitive (numeric/boolean/date) types:

    If .lvwLocation.Tag <> "" Then ...