How to test if an object exists?

D

Darren Linsley

I know this might seem like a dumb question, but how do you test that an
object exists.

Maybe i should explain a little.

I have a variable that points to an object. Now if that oject is released,
how can i test my variable to see if the object it was pointing at, still
exists?

Please help!!
 
A

Arne Janning

Hi Darren!

I know this might seem like a dumb question, but how do you test that an
object exists.

Maybe i should explain a little.

I have a variable that points to an object. Now if that oject is
released,
how can i test my variable to see if the object it was pointing at, still
exists?

Please help!!

If Not myVariable Is Nothing then
.....

Cheers

Arne Janning
 
D

Darren Linsley

I tried that.

The variable is referencing another form.

I am trying to test to see if the form is closed, and if so, re-instanciate
it, prior to performing some actions.

When i test for Nothing once the form has been closed down, it returns
false, indicating the variable reference is still pointing at something. If
i try to reference a property of the form i get an objectdisposedexception.

Any other suggestions??
 
T

Tom Shelton

I tried that.

The variable is referencing another form.

I am trying to test to see if the form is closed, and if so, re-instanciate
it, prior to performing some actions.

When i test for Nothing once the form has been closed down, it returns
false, indicating the variable reference is still pointing at something. If
i try to reference a property of the form i get an objectdisposedexception.

Any other suggestions??

Hook the other forms close event, so that when it closes down you can
set your reference to Nothing, then when you do the test - you'll know
it shutdown...

Just a thought.
 
H

Herfried K. Wagner [MVP]

Darren Linsley said:
I know this might seem like a dumb question, but how do you
test that an object exists.

Maybe i should explain a little.

I have a variable that points to an object. Now if that oject is
released,
how can i test my variable to see if the object it was pointing at, still
exists?

You can check the object's 'IsDisposed' property (for a form, for example).
It's set to 'True' if the object is disposed. Before doing that, compare
the object reference to 'Nothing' ('If o Is Nothing Then...').
 
C

Cor Ligthert

Herfried,

The disposed does not set it to nothing, my expirience with Tom's solution
was strange enough when it was a form better. Although I do not find it nice
so I do not use it and go around it.

Cor
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
The disposed does not set it to nothing

That's true, but you need to check if you ever assigned an instance to the
variable...

But I prefer a solution using a 'Closed' handler or a Singleton too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top