代码之家  ›  专栏  ›  技术社区  ›  SAL

如何知道对象是否已被引用?

  •  1
  • SAL  · 技术社区  · 15 年前

    在附在Excel2003电子表格上的一些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)-不管myobj的状态如何,都跳过“set”。我做不到

    if myObj = Nil then
    

    if myObj = Empty then
    

    if myObj = Nothing then
    

    有什么想法吗?

    萨尔

    1 回复  |  直到 12 年前
        1
  •  6
  •   RBarryYoung    15 年前

    这应该有效:

        If myObj IS Nothing Then
    

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

    推荐文章