Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

 

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

Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
Posted

Is and IsNot

 

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

 

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:

Never trouble another for what you can do for yourself.
  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...