返回属性时未设置错误对象变量或块变量
Posted
技术标签:
【中文标题】返回属性时未设置错误对象变量或块变量【英文标题】: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(...)
【问题讨论】:
【参考方案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
。以上是关于返回属性时未设置错误对象变量或块变量的主要内容,如果未能解决你的问题,请参考以下文章
使用AHK关闭Access应用程序时未设置对象变量(错误91)