NeuralJack Posted February 2, 2007 Posted February 2, 2007 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 Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
MrPaul Posted February 2, 2007 Posted February 2, 2007 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: Quote Never trouble another for what you can do for yourself.
NeuralJack Posted February 2, 2007 Author Posted February 2, 2007 Thanks Mr. Paul! you rock. Quote Currently Using: Visual Basic.Net 2005, .Net Framework 2.0
*Experts* Nerseus Posted February 4, 2007 *Experts* Posted February 4, 2007 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 Quote "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
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.