VBA check if object is set

前端 未结 2 1510
生来不讨喜
生来不讨喜 2020-12-25 09:08

I have a global variable that is an instance of my custom class.

How do I check if the object is set or if I need to initialize it?

2条回答
  •  失恋的感觉
    2020-12-25 09:45

    If obj Is Nothing Then
        ' need to initialize obj: '
        Set obj = ...
    Else
        ' obj already set / initialized. '
    End If
    

    Or, if you prefer it the other way around:

    If Not obj Is Nothing Then
        ' obj already set / initialized. '
    Else
        ' need to initialize obj: '
        Set obj = ...
    End If
    

提交回复
热议问题