HOW TO Use The Obj = Nothing ?

  • Thread starter Thread starter Tiraman
  • Start date Start date
T

Tiraman

Hi ,

i would like to get some explanation about using the Obj = Nothing .

For example ,
I have a function that hold a local object like in this example

public function Test()
dim objMyCol as New Collection()

some code here ....

objMyCol = Nothing ' do i need this line ?
end function

should i use the objMyCol = Nothing before exiting the function Or it will
be set to nothing automatically when the function end ?

one more example ,

I have a class and i have in that class few public/private/shared objects
which are used all over the class .

do i need to close and to set the objects to nothing in the Finalize method
or they will be close and set to nothing automatically ?

does the garbage collector responsible for cleaning unclosed or unused
objects ?

10x in Advanced

Best Regards ,

Tiraman ;-)
 
Tiraman said:
Hi ,

i would like to get some explanation about using the Obj = Nothing
.

For example ,
I have a function that hold a local object like in this example

public function Test()
dim objMyCol as New Collection()

some code here ....

objMyCol = Nothing ' do i need this line ?
end function

should i use the objMyCol = Nothing before exiting the function
No

Or
it will be set to nothing automatically when the function end ?

When the function ends there is nothing that can be set to Nothing because
the variable is destroyed. Cleaning a house that will be destroyed doesn't
make sense. ;-)
one more example ,

I have a class and i have in that class few public/private/shared
objects which are used all over the class .

do i need to close and to set the objects to nothing in the Finalize
method or they will be close and set to nothing automatically ?

No, for the same reason as above: when the object is destroyed, it is
detroyed. It doesn't matter whether a field containing Nothing or anything
else is destroyed.
does the garbage collector responsible for cleaning unclosed or
unused objects ?

Yes, it is. The GC collects all unused objects. Don't know what "unclosed"
means. If managed objects use unmanaged resources, the resources must also
be freed before the managed resources are collected and finalized. Each
object must give these resources free before it get's finalized. In
addition, those objects usually have a Dispose method in order to give the
resources free earlier, i.e. before the GC will collect the object.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
so when we should use the obj = nothing ?I see this as a question on that very good explanation from Armin.
When it was not meant as that than sorry for my response.

I think a good place is after me.close in the main thread, you are than sure
that it does nothing.

It only makes your program than a very little bit bigger.

:-)

Cor
 
so when we should use the obj = nothing ?

10x



Armin Zingler said:
When the function ends there is nothing that can be set to Nothing because
the variable is destroyed. Cleaning a house that will be destroyed doesn't
make sense. ;-)


No, for the same reason as above: when the object is destroyed, it is
detroyed. It doesn't matter whether a field containing Nothing or anything
else is destroyed.


Yes, it is. The GC collects all unused objects. Don't know what "unclosed"
means. If managed objects use unmanaged resources, the resources must also
be freed before the managed resources are collected and finalized. Each
object must give these resources free before it get's finalized. In
addition, those objects usually have a Dispose method in order to give the
resources free earlier, i.e. before the GC will collect the object.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
10x :-)

Cor Ligthert said:
I see this as a question on that very good explanation from Armin.
When it was not meant as that than sorry for my response.

I think a good place is after me.close in the main thread, you are than sure
that it does nothing.

It only makes your program than a very little bit bigger.

:-)

Cor
 
Cor Ligthert said:
I see this as a question on that very good explanation from Armin.

Thanks, Cor. :-)
When it was not meant as that than sorry for my response.

No, it was not meant as a good explanation.... ;-)
I think a good place is after me.close in the main thread, you are
than sure that it does nothing.

It only makes your program than a very little bit bigger.

:-)

:)


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Tiraman,
Armin did a good job of explaining setting objects to nothing.
so when we should use the obj = nothing ?
I primarily use obj = nothing, when I have a persistent (reachable)
variable, that has a reference to an object that is no longer needed. For
example I have a shared field that points to a form, in the close for the
form, I will set the shared field to nothing, as the form is going away.

I will occasionally use obj = nothing, when I am in the middle of a routine
and I want to be certain that I do not reuse an object in a local variable.
However this is rare.

Hope this helps
Jay
 
10x

Jay B. Harlow said:
Tiraman,
Armin did a good job of explaining setting objects to nothing.

I primarily use obj = nothing, when I have a persistent (reachable)
variable, that has a reference to an object that is no longer needed. For
example I have a shared field that points to a form, in the close for the
form, I will set the shared field to nothing, as the form is going away.

I will occasionally use obj = nothing, when I am in the middle of a routine
and I want to be certain that I do not reuse an object in a local variable.
However this is rare.

Hope this helps
Jay
 
Thanks !

Tiraman ;-)


Jay B. Harlow said:
Tiraman,
Armin did a good job of explaining setting objects to nothing.

I primarily use obj = nothing, when I have a persistent (reachable)
variable, that has a reference to an object that is no longer needed. For
example I have a shared field that points to a form, in the close for the
form, I will set the shared field to nothing, as the form is going away.

I will occasionally use obj = nothing, when I am in the middle of a routine
and I want to be certain that I do not reuse an object in a local variable.
However this is rare.

Hope this helps
Jay
 
Back
Top