我如何知道一个对象是否已经被引用?

问题描述 投票:0回答:1

在附加到 Excel 2003 电子表格的某些 VBA 中,我需要使用一些需要一段时间才能实例化的对象 - 所以我只想执行一次“设置”操作...

展示代码比写解释更容易!

' Declare the expensive object as global to this sheet
Dim myObj As SomeBigExpensiveObject

Private Sub CommandButtonDoIt_Click()

   ' Make sure we've got a ref to the object
   If IsEmpty(myObj) Then  ' this doesn't work!
      Set myObj = New SomeBigExpensiveObject
   End If

   ' ... etc

End Sub

如何检查 myObj 是否已设置?

我尝试过 IsNull(myObj) 和 IsEmpty(myObj) - 两者都会跳过“set”,无论 myObj 的状态如何。我做不到

if myObj = Nil then

if myObj = Empty then

if myObj = Nothing then

有什么想法吗?

excel vba excel-2003
1个回答
6
投票

这应该有效:

    If myObj IS Nothing Then

(注意“IS”)如果这不起作用,则必须由该类专门实现异步初始化,因为默认情况下 COM 初始化调用是同步的。因此,您需要检查文档,或者与开发人员讨论 Big 类的某些属性或同步方法,以便您等待。

© www.soinside.com 2019 - 2024. All rights reserved.