Testing a Variable for Instantiation [vb.net]

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
Sometimes when I define a variable holding a class I dont want to instantiate it until later.. possibly in another function even. This means for safety I'd like to be able to test that it's been instantiated before I acutally use it, to avoid errors. So how can I test for instantiation of a class I made?

Visual Basic:
Dim Bob as MyClass  'Not instantiated

'So i want something like ..
If Bob <> Nothing Then
'Continue..
End if
'But i'm pretty sure that <> Nothing doesnt work on classes
 
Is and IsNot

You can compare references in VB.Net using Is and IsNot:

Visual Basic:
If (bob Is Nothing) Then
    'Not instantiated
End If

If (bob IsNot Nothing) Then
    'Instantiated
End If

If (bob Is bill) Then
    'bob and bill reference the same object
End If

'etc

Good luck :cool:
 
If you didn't already know, what you're doing is generally called "lazy loading" or similar. The idea is only instantiate the object when it's needed. Thought I'd throw out that term in case you found it useful in google searches.

-ner
 
Back
Top