Search code examples
comvb6

Error Object variable or with block variable not set while returning property


I'm trying to use my COM class in VB6 application. I made some kind of wrapper. When I create object I see message Is Nothing 1, so it suggests that member variable is set and it's not null, but when I try return this member using property, then in return line I see exception Object variable or with block variable not set. I have no idea how to fix it.

// MyComClass
This is a COM class with method Operation

// clsMyCom.cls
Private WithEvents m_myComClass As MyComClass

Private Sub Class_Initialize()
        
    If m_myComClass Is Nothing Then
        MsgBox "Is Nothing 1"
    End If
    
    Set m_myComClass = New MyComClass
    
    If m_myComClass Is Nothing Then
        MsgBox "Is Nothing 2"
    End If

End Sub

Public Property Get MyImplementation() As MyComClass

    If m_myComClass Is Nothing Then
        MsgBox "Is Nothing 3"
    End If
    
    // in this line I see exception:
    // object variable or with block variable not set
    MyImplementation = m_myComClass

End  Property

// usage
Dim variable As clsMyCom
Set variable = New clsMyCom
Call variable.MyImplementation.Operation(...)

Solution

  • Change the method to this:

    Public Property Get MyImplementation() As MyComClass
    
        If m_myComClass Is Nothing Then
            MsgBox "Is Nothing 3"
        End If
        
        // in this line I see exception:
        // object variable or with block variable not set
        Set MyImplementation = m_myComClass
    
    End  Property
    

    To be clear, you missed the Set