Hello ,,,
Well you have started something

....... this has been discussed
manny times in this group and it always ends the same
The below is my opinion , wich is formed by following a lot of these
discussions and how it is described in the manny books i own
The BIG NO NO is a bit odd , he would be right if he would have said
that it isn`t necesary and that you are typing a line to much
if your object is declared in method scope
but there are situations thinkable where it is valid to set a object to
Nothing ( however never absolut necesary , as it could have been in
VB6 )
so here are some scenario`s for you to rethink :
Example A
Private sub Useobject ()
dim foo as new Objfoo.lib
---- use the foo object
--- setting the foo object here to nothing is absolute not necesary
--- as soon as it runs out of scope it is marked for collection
end sub
Example B
Private sub Useobject ()
dim foo as new Objfoo.lib
for i as integer = 0 to 1000
if i < 500 then
---- use the foo object
else
---- stop using the foo object
end if
next
end sub
--- here is a situation where it is valid to set the foo object to nothing
in the else statement ( if i = 500 then foo=Nothing )
--- especially when it is a resource hungry object , However it is not
guaranteed that the object is collected at this point
end sub
Example C
The object is declared in class scope ( because you use it withevents or
whatever reasson you might have )
and for some reasson you can live without it , for a time and if needed
you can declare a new instance
i encountered this scenario myself in a remoting scenario wich used a
singleton
if you use VB.Net 2005 then learn yourself to use the using stetement
( it makes sure everything is nicely cleaned up , and as a bonus it makes
your code nicer readable )
example
Private sub Useobject ()
using foo as new Objfoo.lib
---- use the foo object
end using
end sub
As said the above is my own opinion without warranty`s especially about
example B i get a lot of nasty responses however it is a Balena example
written in the core reference guide of VB.net,, so i guess it should be a
valid situation .
I know this thread is going to explode ( as it normally does on this
subject ) with all people telling you something different , i would say
form your own opinion
by reading about the GC on MSDN then you will see what it does and what
it doesn`t , and especially this is verry important , setting a object to
Nothing does not mean that you will get your resources inmediatly back ,
however in resource hungry systems it might make a difference in the right
scenario .
regards
Michel Posseth [MCP]
Hi all,
Coming from the good old VB6 days we were told to always destroy our
objects as follows:-
Dim obj as New MyObject
' do some work with obj
obj = Nothing
I have been doing this in .Net also for quite a while now. The other day
one of my peers mentioned that this is a BIG NO NO and that I should let
the Garbage Collection process handle this for me.
Can someone share any insight on this?
TIA!