【问题标题】:Error Object variable or with block variable not set while returning property返回属性时未设置错误对象变量或块变量
【发布时间】:2021-01-16 10:59:13
【问题描述】:

我正在尝试在 VB6 应用程序中使用我的 COM 类。我做了某种包装。当我创建对象时,我看到消息Is Nothing 1,因此它表明已设置成员变量并且它不为空,但是当我尝试使用属性返回此成员时,然后在返回行中我看到异常Object variable or with block variable not set。我不知道如何解决它。

// 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(...)

【问题讨论】:

    标签: com vb6


    【解决方案1】:

    把方法改成这样:

    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
    

    需要明确的是,您错过了Set

    【讨论】:

    • 我不知道返回对象时Property中需要Set
    • MyImplementation = <<expr>> 分配给对象的默认属性(如果有),例如MyImplementation.Text = "some string" 的简写。错误“需要对象...”意味着 MyImplementation 在尝试分配给其默认属性时是 Nothing
    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多