105

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?

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Icode4food
  • 8,504
  • 16
  • 61
  • 93

3 Answers3

161
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
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 2
    I knew it had to be simple when I Googled it and found nothing! Thanks for your help! – Icode4food Apr 13 '10 at 18:53
  • 3
    Note that checking `obj Is Nothing` is NOT the same as checking `IsNothing(obj)`! Thanks for the correct syntax to check this...not sure why `IsNothing()` behaves differently... – Matt Browne May 14 '13 at 22:11
  • 2
    I find `Not (obj Is Nothing)` easier to understand than `Not obj Is Nothing`. My brain doesn't know what a "Not obj" is! – Martin F Nov 16 '15 at 22:37
  • 1
    You can also write : If obj IsNot Nothing which I find it much more clearer than the rest. It also kinda feel the same as C# (ojb != null) – Alexandre Jun 23 '16 at 14:15
  • Thanks for the short and clear answer! IMO, the straight read "if obj is nothing then create the obj else exit" is the best human readable syntax. The double negation is weird, even in real human relations... – Hristo Jul 21 '23 at 06:05
5

The (un)safe way to do this - if you are ok with not using option explicit - is...

Not TypeName(myObj) = "Empty"

This also handles the case if the object has not been declared. This is useful if you want to just comment out a declaration to switch off some behaviour...

Dim myObj as Object
Not TypeName(myObj) = "Empty"  '/ true, the object exists - TypeName is Object

'Dim myObj as Object
Not TypeName(myObj) = "Empty"  '/ false, the object has not been declared

This works because VBA will auto-instantiate an undeclared variable as an Empty Variant type. It eliminates the need for an auxiliary Boolean to manage the behaviour.

Cool Blue
  • 6,438
  • 6
  • 29
  • 68
  • 5
    Nobody should ever have any VBA code without `Option Explicit`. It gains nothing except problems. To "switch" behavior, use Conditional Compiling. – Andre Nov 03 '16 at 11:18
  • @andre, yes, fair point. I feel ok without it because I use hungarian notation for scope, but I try to avoid vba these days if I can. Most of what I see is about explicit declarations, name safety and avoiding the dreaded variants. What are your key reasons? – Cool Blue Nov 03 '16 at 13:10
  • In fact i get "Nothing" as result of typename , and not "Empty" – Patrick Lepelletier Mar 14 '19 at 11:56
5

When using global variables it's possible to run into a situation in which the object is empty. So, the code:

If Not obj Is Nothing Then
  'obj is already set
Else
  'set obj
End If

produces an 'object required' error.

In this situation, the following works:

'First check it is initialized 
If IsObject(obj) Then
     'Then check if it is set
     If Not obj Is Nothing Then
        'obj is set
     Else
        'set obj
     End If
End If
Amy
  • 165
  • 1
  • 10