Home All Groups Group Topic Archive Search About
Author
19 Mar 2006 8:52 AM
andreas
Hi,
I am working with the streams objects (filestream,streamwriter,streamreader)
May I do or is it good programming to make a sub like :

public sub Closestreams(ByRef ob as object)
if not (ob is nothing) then
ob.close
ob = nothing
end if
end sub

thanks for any response

Author
19 Mar 2006 9:30 AM
Cor Ligthert [MVP]
Andreas,

Is a simple close not more effective. The ob = nothing has no sense at all.

Cor

Show quoteHide quote
"andreas" <andr***@pandora.be> schreef in bericht
news:DZ8Tf.324646$4A1.10358835@phobos.telenet-ops.be...
> Hi,
> I am working with the streams objects
> (filestream,streamwriter,streamreader)
> May I do or is it good programming to make a sub like :
>
> public sub Closestreams(ByRef ob as object)
> if not (ob is nothing) then
> ob.close
> ob = nothing
> end if
> end sub
>
> thanks for any response
>
>
Author
19 Mar 2006 10:57 AM
Michel Posseth [MCP]
Well in my opinion this depends on the usage in his code of the object

example:

sub DoSomeStuff ()
Dim  objVar  as new foo

'use the object
.....
No need to explicitly set the var to Nothing

End Sub

'-----in this situation it makes sense to use Nothing

sub DoSomeStuff ()
Dim  objVar  as new foo
for i as integer = 1 to 1000
if i <= 50 then
objvar.PerformsomeAction
if  i=50 then objVar = Nothing
else
'do something else except using ObjVar
end if
next

End Sub


regards

Michel Posseth [MCP]








Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:OiFh2dzSGHA.4752@TK2MSFTNGP10.phx.gbl...
> Andreas,
>
> Is a simple close not more effective. The ob = nothing has no sense at
> all.
>
> Cor
>
> "andreas" <andr***@pandora.be> schreef in bericht
> news:DZ8Tf.324646$4A1.10358835@phobos.telenet-ops.be...
>> Hi,
>> I am working with the streams objects
>> (filestream,streamwriter,streamreader)
>> May I do or is it good programming to make a sub like :
>>
>> public sub Closestreams(ByRef ob as object)
>> if not (ob is nothing) then
>> ob.close
>> ob = nothing
>> end if
>> end sub
>>
>> thanks for any response
>>
>>
>
>
Author
19 Mar 2006 11:21 AM
Cor Ligthert [MVP]
Michel,,

If it was this code (and I don't believe somebody uses this with a stream
object

Private a as streamwriter
Private sub b
a = new streamwriter
etc
End Sub

than it could have sense, however in my opinion is this not so clever code

Normally and in your example does the sub goes out of scope and will be
released by the GC.

In your sample if there is set a reference from an other object to this
object (and therefore will not be released to whatever nothing you set it),
than setting to nothing does not add much, it goes out of scope if it has or
has and not a reference.

Just my thought,

Cor


Show quoteHide quote
"Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
news:OnAieP0SGHA.5116@TK2MSFTNGP14.phx.gbl...
> Well in my opinion this depends on the usage in his code of the object
>
> example:
>
> sub DoSomeStuff ()
> Dim  objVar  as new foo
>
> 'use the object
> ....
> No need to explicitly set the var to Nothing
>
> End Sub
>
> '-----in this situation it makes sense to use Nothing
>
> sub DoSomeStuff ()
> Dim  objVar  as new foo
> for i as integer = 1 to 1000
> if i <= 50 then
> objvar.PerformsomeAction
> if  i=50 then objVar = Nothing
> else
> 'do something else except using ObjVar
> end if
> next
>
> End Sub
>
>
> regards
>
> Michel Posseth [MCP]
>
>
>
>
>
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
> news:OiFh2dzSGHA.4752@TK2MSFTNGP10.phx.gbl...
>> Andreas,
>>
>> Is a simple close not more effective. The ob = nothing has no sense at
>> all.
>>
>> Cor
>>
>> "andreas" <andr***@pandora.be> schreef in bericht
>> news:DZ8Tf.324646$4A1.10358835@phobos.telenet-ops.be...
>>> Hi,
>>> I am working with the streams objects
>>> (filestream,streamwriter,streamreader)
>>> May I do or is it good programming to make a sub like :
>>>
>>> public sub Closestreams(ByRef ob as object)
>>> if not (ob is nothing) then
>>> ob.close
>>> ob = nothing
>>> end if
>>> end sub
>>>
>>> thanks for any response
>>>
>>>
>>
>>
>
>
Author
20 Mar 2006 6:09 AM
Michel Posseth [MCP]
I agre with most of it ,,, but i guess you did not look carefully to example
2 ( where it is valid to use Nothing )

as you see we are in a loop with a codition set for usage of the object ,,,
setting it to nothing after finished with it , makes it possible to collect
the object before the method / loop has finished .    As we never know when
the GC kicks in it might also not do this i am aware of that however on a
resource hungry system it might make a difference


regards

Michel Posseth [MCP]



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
news:e6WeAc0SGHA.4300@TK2MSFTNGP14.phx.gbl...
> Michel,,
>
> If it was this code (and I don't believe somebody uses this with a stream
> object
>
> Private a as streamwriter
> Private sub b
> a = new streamwriter
> etc
> End Sub
>
> than it could have sense, however in my opinion is this not so clever code
>
> Normally and in your example does the sub goes out of scope and will be
> released by the GC.
>
> In your sample if there is set a reference from an other object to this
> object (and therefore will not be released to whatever nothing you set
> it), than setting to nothing does not add much, it goes out of scope if it
> has or has and not a reference.
>
> Just my thought,
>
> Cor
>
>
> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
> news:OnAieP0SGHA.5116@TK2MSFTNGP14.phx.gbl...
>> Well in my opinion this depends on the usage in his code of the object
>>
>> example:
>>
>> sub DoSomeStuff ()
>> Dim  objVar  as new foo
>>
>> 'use the object
>> ....
>> No need to explicitly set the var to Nothing
>>
>> End Sub
>>
>> '-----in this situation it makes sense to use Nothing
>>
>> sub DoSomeStuff ()
>> Dim  objVar  as new foo
>> for i as integer = 1 to 1000
>> if i <= 50 then
>> objvar.PerformsomeAction
>> if  i=50 then objVar = Nothing
>> else
>> 'do something else except using ObjVar
>> end if
>> next
>>
>> End Sub
>>
>>
>> regards
>>
>> Michel Posseth [MCP]
>>
>>
>>
>>
>>
>>
>>
>>
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht
>> news:OiFh2dzSGHA.4752@TK2MSFTNGP10.phx.gbl...
>>> Andreas,
>>>
>>> Is a simple close not more effective. The ob = nothing has no sense at
>>> all.
>>>
>>> Cor
>>>
>>> "andreas" <andr***@pandora.be> schreef in bericht
>>> news:DZ8Tf.324646$4A1.10358835@phobos.telenet-ops.be...
>>>> Hi,
>>>> I am working with the streams objects
>>>> (filestream,streamwriter,streamreader)
>>>> May I do or is it good programming to make a sub like :
>>>>
>>>> public sub Closestreams(ByRef ob as object)
>>>> if not (ob is nothing) then
>>>> ob.close
>>>> ob = nothing
>>>> end if
>>>> end sub
>>>>
>>>> thanks for any response
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
19 Mar 2006 12:37 PM
Herfried K. Wagner [MVP]
"andreas" <andr***@pandora.be> schrieb:
> I am working with the streams objects
> (filestream,streamwriter,streamreader)
> May I do or is it good programming to make a sub like :
>
> public sub Closestreams(ByRef ob as object)
> if not (ob is nothing) then
> ob.close
> ob = nothing
> end if
> end sub

(1) I am curious why you are passing 'ob' in as 'ByRef'.  I suggest to
    change 'ByRef' to 'ByVal'.

(2) Why are you typing 'ob' as 'Object' and not as 'Stream' or similar?

(3) Setting the variable to nothing doesn't make much sense here...

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
19 Mar 2006 2:12 PM
andreas
I write the whole thing (with try and so on)

dim fs as new filestream(.......)
dim sw as new streamwriter(fs)
... some code with sw
dim ffss as new filestream(...)
dim ssww as new streamreader(ffss)
....some code with ssww

and now I want to close savely the whole thing
closestreams(sw)
closestreams(fs)
closestreams(ssww)
closestreams(ffss)

and now I repeat my question about the sub closestreams


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:#iQrkH1SGHA.4492@TK2MSFTNGP09.phx.gbl...
> "andreas" <andr***@pandora.be> schrieb:
> > I am working with the streams objects
> > (filestream,streamwriter,streamreader)
> > May I do or is it good programming to make a sub like :
> >
> > public sub Closestreams(ByRef ob as object)
> > if not (ob is nothing) then
> > ob.close
> > ob = nothing
> > end if
> > end sub
>
> (1) I am curious why you are passing 'ob' in as 'ByRef'.  I suggest to
>     change 'ByRef' to 'ByVal'.
>
> (2) Why are you typing 'ob' as 'Object' and not as 'Stream' or similar?
>
> (3) Setting the variable to nothing doesn't make much sense here...
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
Author
19 Mar 2006 2:39 PM
Cor Ligthert [MVP]
Andreas,

What is in your opinion the advantage?

I do not see any, I see only disadvantages.

Cor

Show quoteHide quote
"andreas" <andr***@pandora.be> schreef in bericht
news:ZEdTf.325175$Bu.10140738@phobos.telenet-ops.be...
>I write the whole thing (with try and so on)
>
> dim fs as new filestream(.......)
> dim sw as new streamwriter(fs)
> .. some code with sw
> dim ffss as new filestream(...)
> dim ssww as new streamreader(ffss)
> ...some code with ssww
>
> and now I want to close savely the whole thing
> closestreams(sw)
> closestreams(fs)
> closestreams(ssww)
> closestreams(ffss)
>
> and now I repeat my question about the sub closestreams
>
>
> "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
> news:#iQrkH1SGHA.4492@TK2MSFTNGP09.phx.gbl...
>> "andreas" <andr***@pandora.be> schrieb:
>> > I am working with the streams objects
>> > (filestream,streamwriter,streamreader)
>> > May I do or is it good programming to make a sub like :
>> >
>> > public sub Closestreams(ByRef ob as object)
>> > if not (ob is nothing) then
>> > ob.close
>> > ob = nothing
>> > end if
>> > end sub
>>
>> (1) I am curious why you are passing 'ob' in as 'ByRef'.  I suggest to
>>     change 'ByRef' to 'ByVal'.
>>
>> (2) Why are you typing 'ob' as 'Object' and not as 'Stream' or similar?
>>
>> (3) Setting the variable to nothing doesn't make much sense here...
>>
>> --
>>  M S   Herfried K. Wagner
>> M V P  <URL:http://dotnet.mvps.org/>
>>  V B   <URL:http://classicvb.org/petition/>
>>
>
>
Author
19 Mar 2006 3:31 PM
David Anton
As Herfried says, it's pointless to set ob to Nothing - ob is just a
parameter that goes out of scope immediately after you are setting it to
Nothing, so that's unnecessary.  Excessive setting objects to Nothing is a
holdover from classic VB (and even there it's not required as much as people
think).  Also, setting it Nothing in that method does not remove the
reference in the calling method - but even there it's not practical unless
your calling method is so long that you're worried about encouraging garbage
collection as soon as possible (even then, "Using" is a more direct approach
if that's your concern).

So, all you're left with in your function is:
if not (ob is nothing) then
  ob.close

I think just using the Close methods on the objects directly from your
calling method is cleaner.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



Show quoteHide quote
"andreas" wrote:

> Hi,
> I am working with the streams objects (filestream,streamwriter,streamreader)
> May I do or is it good programming to make a sub like :
>
> public sub Closestreams(ByRef ob as object)
> if not (ob is nothing) then
> ob.close
> ob = nothing
> end if
> end sub
>
> thanks for any response
>
>
>
Author
20 Mar 2006 6:05 AM
Michel Posseth [MCP]
Well ......


It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i see
Nothing used even better i found some articles that state that in some
situations it might even benefit the GC to use Nothing .

Can you clean to good in your home ???

and indeed as a VB6 progger i learned how to clean up my mess ....  :-)

in the example i showed in a  above post for instance it is in my opinion
perfectly valid to use Nothing , as it might help in the release of the
object before the method has finished

regards

Michel Posseth [MCP]



Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
> As Herfried says, it's pointless to set ob to Nothing - ob is just a
> parameter that goes out of scope immediately after you are setting it to
> Nothing, so that's unnecessary.  Excessive setting objects to Nothing is a
> holdover from classic VB (and even there it's not required as much as
> people
> think).  Also, setting it Nothing in that method does not remove the
> reference in the calling method - but even there it's not practical unless
> your calling method is so long that you're worried about encouraging
> garbage
> collection as soon as possible (even then, "Using" is a more direct
> approach
> if that's your concern).
>
> So, all you're left with in your function is:
> if not (ob is nothing) then
>  ob.close
>
> I think just using the Close methods on the objects directly from your
> calling method is cleaner.
> --
> David Anton
> www.tangiblesoftwaresolutions.com
> Instant C#: VB to C# converter
> Instant VB: C# to VB converter
> Instant C++: C# to C++ converter & VB to C++ converter
> Instant J#: VB to J# converter
>
>
>
> "andreas" wrote:
>
>> Hi,
>> I am working with the streams objects
>> (filestream,streamwriter,streamreader)
>> May I do or is it good programming to make a sub like :
>>
>> public sub Closestreams(ByRef ob as object)
>> if not (ob is nothing) then
>> ob.close
>> ob = nothing
>> end if
>> end sub
>>
>> thanks for any response
>>
>>
>>
Author
20 Mar 2006 7:27 AM
Cor Ligthert [MVP]
Michel,

I answer your question in your other reply here. AFAIK does the GC not start
in the middle of a routine. That is AFAIK the only reason to start the GC by
hand. This with as exception that the action in your sample is a kind of IO
operation (including painting). Than I agree that it can be a reason to do
as you show, however, than I would set in your sample the force of the GC. I
did not see now that you did mean an IO (or operation as that).

Cor


Show quoteHide quote
"Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>
> Well ......
>
>
> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i
> see Nothing used even better i found some articles that state that in some
> situations it might even benefit the GC to use Nothing .
>
> Can you clean to good in your home ???
>
> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>
> in the example i showed in a  above post for instance it is in my opinion
> perfectly valid to use Nothing , as it might help in the release of the
> object before the method has finished
>
> regards
>
> Michel Posseth [MCP]
>
>
>
> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>> parameter that goes out of scope immediately after you are setting it to
>> Nothing, so that's unnecessary.  Excessive setting objects to Nothing is
>> a
>> holdover from classic VB (and even there it's not required as much as
>> people
>> think).  Also, setting it Nothing in that method does not remove the
>> reference in the calling method - but even there it's not practical
>> unless
>> your calling method is so long that you're worried about encouraging
>> garbage
>> collection as soon as possible (even then, "Using" is a more direct
>> approach
>> if that's your concern).
>>
>> So, all you're left with in your function is:
>> if not (ob is nothing) then
>>  ob.close
>>
>> I think just using the Close methods on the objects directly from your
>> calling method is cleaner.
>> --
>> David Anton
>> www.tangiblesoftwaresolutions.com
>> Instant C#: VB to C# converter
>> Instant VB: C# to VB converter
>> Instant C++: C# to C++ converter & VB to C++ converter
>> Instant J#: VB to J# converter
>>
>>
>>
>> "andreas" wrote:
>>
>>> Hi,
>>> I am working with the streams objects
>>> (filestream,streamwriter,streamreader)
>>> May I do or is it good programming to make a sub like :
>>>
>>> public sub Closestreams(ByRef ob as object)
>>> if not (ob is nothing) then
>>> ob.close
>>> ob = nothing
>>> end if
>>> end sub
>>>
>>> thanks for any response
>>>
>>>
>>>
>
>
Author
20 Mar 2006 12:26 PM
m.posseth
Cor ,


>AFAIK does the GC not start in the middle of a routine.

well i wonder if it doesn`t ,,, ,, cause


this code

sub DoSomeStuff ()
Dim  objVar  as new foo
for i as integer = 1 to 1000
if i <= 50 then
objvar.PerformsomeAction
if  i=50 then objVar = Nothing
else
'do something else except using ObjVar
end if
next

End Sub


Can be found in the best practices guidelines  from MS

So i guess the reasson is that objVar can be released before it runs out of
scope   ( before the method ends ) otherwise some writers at MS made some
serious flaws in there documentation



regards

Michel Posseth [MCP]



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:OvsjH%23%23SGHA.4976@TK2MSFTNGP11.phx.gbl...
> Michel,
>
> I answer your question in your other reply here. AFAIK does the GC not
> start in the middle of a routine. That is AFAIK the only reason to start
> the GC by hand. This with as exception that the action in your sample is a
> kind of IO operation (including painting). Than I agree that it can be a
> reason to do as you show, however, than I would set in your sample the
> force of the GC. I did not see now that you did mean an IO (or operation
> as that).
>
> Cor
>
>
> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
> news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>>
>> Well ......
>>
>>
>> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i
>> see Nothing used even better i found some articles that state that in
>> some situations it might even benefit the GC to use Nothing .
>>
>> Can you clean to good in your home ???
>>
>> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>>
>> in the example i showed in a  above post for instance it is in my opinion
>> perfectly valid to use Nothing , as it might help in the release of the
>> object before the method has finished
>>
>> regards
>>
>> Michel Posseth [MCP]
>>
>>
>>
>> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
>> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>>> parameter that goes out of scope immediately after you are setting it to
>>> Nothing, so that's unnecessary.  Excessive setting objects to Nothing is
>>> a
>>> holdover from classic VB (and even there it's not required as much as
>>> people
>>> think).  Also, setting it Nothing in that method does not remove the
>>> reference in the calling method - but even there it's not practical
>>> unless
>>> your calling method is so long that you're worried about encouraging
>>> garbage
>>> collection as soon as possible (even then, "Using" is a more direct
>>> approach
>>> if that's your concern).
>>>
>>> So, all you're left with in your function is:
>>> if not (ob is nothing) then
>>>  ob.close
>>>
>>> I think just using the Close methods on the objects directly from your
>>> calling method is cleaner.
>>> --
>>> David Anton
>>> www.tangiblesoftwaresolutions.com
>>> Instant C#: VB to C# converter
>>> Instant VB: C# to VB converter
>>> Instant C++: C# to C++ converter & VB to C++ converter
>>> Instant J#: VB to J# converter
>>>
>>>
>>>
>>> "andreas" wrote:
>>>
>>>> Hi,
>>>> I am working with the streams objects
>>>> (filestream,streamwriter,streamreader)
>>>> May I do or is it good programming to make a sub like :
>>>>
>>>> public sub Closestreams(ByRef ob as object)
>>>> if not (ob is nothing) then
>>>> ob.close
>>>> ob = nothing
>>>> end if
>>>> end sub
>>>>
>>>> thanks for any response
>>>>
>>>>
>>>>
>>
>>
>
>
Author
20 Mar 2006 12:50 PM
Cor Ligthert [MVP]
Michel,

If the method from objvar.PerformsomeAction is calling the GC or by instance
do a show of whatever, than it works in my opinion.

:-)

Cor


Show quoteHide quote
"m.posseth" <mich***@nohausystems.nl> schreef in bericht
news:eK$DBmBTGHA.5656@TK2MSFTNGP11.phx.gbl...
> Cor ,
>
>
>>AFAIK does the GC not start in the middle of a routine.
>
> well i wonder if it doesn`t ,,, ,, cause
>
>
> this code
>
> sub DoSomeStuff ()
> Dim  objVar  as new foo
> for i as integer = 1 to 1000
> if i <= 50 then
> objvar.PerformsomeAction
> if  i=50 then objVar = Nothing
> else
> 'do something else except using ObjVar
> end if
> next
>
> End Sub
>
>
> Can be found in the best practices guidelines  from MS
>
> So i guess the reasson is that objVar can be released before it runs out
> of scope   ( before the method ends ) otherwise some writers at MS made
> some serious flaws in there documentation
>
>
>
> regards
>
> Michel Posseth [MCP]
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:OvsjH%23%23SGHA.4976@TK2MSFTNGP11.phx.gbl...
>> Michel,
>>
>> I answer your question in your other reply here. AFAIK does the GC not
>> start in the middle of a routine. That is AFAIK the only reason to start
>> the GC by hand. This with as exception that the action in your sample is
>> a kind of IO operation (including painting). Than I agree that it can be
>> a reason to do as you show, however, than I would set in your sample the
>> force of the GC. I did not see now that you did mean an IO (or operation
>> as that).
>>
>> Cor
>>
>>
>> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
>> news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>>>
>>> Well ......
>>>
>>>
>>> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i
>>> see Nothing used even better i found some articles that state that in
>>> some situations it might even benefit the GC to use Nothing .
>>>
>>> Can you clean to good in your home ???
>>>
>>> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>>>
>>> in the example i showed in a  above post for instance it is in my
>>> opinion perfectly valid to use Nothing , as it might help in the release
>>> of the object before the method has finished
>>>
>>> regards
>>>
>>> Michel Posseth [MCP]
>>>
>>>
>>>
>>> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
>>> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>>>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>>>> parameter that goes out of scope immediately after you are setting it
>>>> to
>>>> Nothing, so that's unnecessary.  Excessive setting objects to Nothing
>>>> is a
>>>> holdover from classic VB (and even there it's not required as much as
>>>> people
>>>> think).  Also, setting it Nothing in that method does not remove the
>>>> reference in the calling method - but even there it's not practical
>>>> unless
>>>> your calling method is so long that you're worried about encouraging
>>>> garbage
>>>> collection as soon as possible (even then, "Using" is a more direct
>>>> approach
>>>> if that's your concern).
>>>>
>>>> So, all you're left with in your function is:
>>>> if not (ob is nothing) then
>>>>  ob.close
>>>>
>>>> I think just using the Close methods on the objects directly from your
>>>> calling method is cleaner.
>>>> --
>>>> David Anton
>>>> www.tangiblesoftwaresolutions.com
>>>> Instant C#: VB to C# converter
>>>> Instant VB: C# to VB converter
>>>> Instant C++: C# to C++ converter & VB to C++ converter
>>>> Instant J#: VB to J# converter
>>>>
>>>>
>>>>
>>>> "andreas" wrote:
>>>>
>>>>> Hi,
>>>>> I am working with the streams objects
>>>>> (filestream,streamwriter,streamreader)
>>>>> May I do or is it good programming to make a sub like :
>>>>>
>>>>> public sub Closestreams(ByRef ob as object)
>>>>> if not (ob is nothing) then
>>>>> ob.close
>>>>> ob = nothing
>>>>> end if
>>>>> end sub
>>>>>
>>>>> thanks for any response
>>>>>
>>>>>
>>>>>
>>>
>>>
>>
>>
>
>
Author
20 Mar 2006 1:08 PM
m.posseth
Well if i read this

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbup1029.asp


I translate this " if an object is set to Nothing inside a procedure and the
next line of code creates an object of the same name, the first object may
not yet be destroyed "

That it might or might not be destroyed at this point ,,,,  so afaik the
carbage collector kicks in whenever it feels that it is necesary to do so
( resources needed ) and so it becomes in my opinion again good practice to
release the object as soon as you are ready with it and it makes sense to do
so   ( like my loop example ) .


or am i missing something here ???


regards

Michel







Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:e4FdmyBTGHA.1576@tk2msftngp13.phx.gbl...
> Michel,
>
> If the method from objvar.PerformsomeAction is calling the GC or by
> instance do a show of whatever, than it works in my opinion.
>
> :-)
>
> Cor
>
>
> "m.posseth" <mich***@nohausystems.nl> schreef in bericht
> news:eK$DBmBTGHA.5656@TK2MSFTNGP11.phx.gbl...
>> Cor ,
>>
>>
>>>AFAIK does the GC not start in the middle of a routine.
>>
>> well i wonder if it doesn`t ,,, ,, cause
>>
>>
>> this code
>>
>> sub DoSomeStuff ()
>> Dim  objVar  as new foo
>> for i as integer = 1 to 1000
>> if i <= 50 then
>> objvar.PerformsomeAction
>> if  i=50 then objVar = Nothing
>> else
>> 'do something else except using ObjVar
>> end if
>> next
>>
>> End Sub
>>
>>
>> Can be found in the best practices guidelines  from MS
>>
>> So i guess the reasson is that objVar can be released before it runs out
>> of scope   ( before the method ends ) otherwise some writers at MS made
>> some serious flaws in there documentation
>>
>>
>>
>> regards
>>
>> Michel Posseth [MCP]
>>
>>
>>
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>> news:OvsjH%23%23SGHA.4976@TK2MSFTNGP11.phx.gbl...
>>> Michel,
>>>
>>> I answer your question in your other reply here. AFAIK does the GC not
>>> start in the middle of a routine. That is AFAIK the only reason to start
>>> the GC by hand. This with as exception that the action in your sample is
>>> a kind of IO operation (including painting). Than I agree that it can be
>>> a reason to do as you show, however, than I would set in your sample the
>>> force of the GC. I did not see now that you did mean an IO (or operation
>>> as that).
>>>
>>> Cor
>>>
>>>
>>> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
>>> news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>>>>
>>>> Well ......
>>>>
>>>>
>>>> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i
>>>> see Nothing used even better i found some articles that state that in
>>>> some situations it might even benefit the GC to use Nothing .
>>>>
>>>> Can you clean to good in your home ???
>>>>
>>>> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>>>>
>>>> in the example i showed in a  above post for instance it is in my
>>>> opinion perfectly valid to use Nothing , as it might help in the
>>>> release of the object before the method has finished
>>>>
>>>> regards
>>>>
>>>> Michel Posseth [MCP]
>>>>
>>>>
>>>>
>>>> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
>>>> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>>>>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>>>>> parameter that goes out of scope immediately after you are setting it
>>>>> to
>>>>> Nothing, so that's unnecessary.  Excessive setting objects to Nothing
>>>>> is a
>>>>> holdover from classic VB (and even there it's not required as much as
>>>>> people
>>>>> think).  Also, setting it Nothing in that method does not remove the
>>>>> reference in the calling method - but even there it's not practical
>>>>> unless
>>>>> your calling method is so long that you're worried about encouraging
>>>>> garbage
>>>>> collection as soon as possible (even then, "Using" is a more direct
>>>>> approach
>>>>> if that's your concern).
>>>>>
>>>>> So, all you're left with in your function is:
>>>>> if not (ob is nothing) then
>>>>>  ob.close
>>>>>
>>>>> I think just using the Close methods on the objects directly from your
>>>>> calling method is cleaner.
>>>>> --
>>>>> David Anton
>>>>> www.tangiblesoftwaresolutions.com
>>>>> Instant C#: VB to C# converter
>>>>> Instant VB: C# to VB converter
>>>>> Instant C++: C# to C++ converter & VB to C++ converter
>>>>> Instant J#: VB to J# converter
>>>>>
>>>>>
>>>>>
>>>>> "andreas" wrote:
>>>>>
>>>>>> Hi,
>>>>>> I am working with the streams objects
>>>>>> (filestream,streamwriter,streamreader)
>>>>>> May I do or is it good programming to make a sub like :
>>>>>>
>>>>>> public sub Closestreams(ByRef ob as object)
>>>>>> if not (ob is nothing) then
>>>>>> ob.close
>>>>>> ob = nothing
>>>>>> end if
>>>>>> end sub
>>>>>>
>>>>>> thanks for any response
>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
20 Mar 2006 1:46 PM
Cor Ligthert [MVP]
Michael,


Michael,

and so it becomes in my opinion again good practice to release the object
as soon as you are ready with it

What is the good practise in this, somebody who started with this fairytale

As long as there is more than enough resource and memory there is in my
opinion not any need to clean the older up.

It is not your desktop it is your garbage. You only need to put it at the
door when there is not enough room more and/or that you are unsure if there
is enough room until the next garbage is collected by the garbageman.

Cor

Show quoteHide quote
>
>
> or am i missing something here ???
>
>
> regards
>
> Michel
>
>
>
>
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:e4FdmyBTGHA.1576@tk2msftngp13.phx.gbl...
>> Michel,
>>
>> If the method from objvar.PerformsomeAction is calling the GC or by
>> instance do a show of whatever, than it works in my opinion.
>>
>> :-)
>>
>> Cor
>>
>>
>> "m.posseth" <mich***@nohausystems.nl> schreef in bericht
>> news:eK$DBmBTGHA.5656@TK2MSFTNGP11.phx.gbl...
>>> Cor ,
>>>
>>>
>>>>AFAIK does the GC not start in the middle of a routine.
>>>
>>> well i wonder if it doesn`t ,,, ,, cause
>>>
>>>
>>> this code
>>>
>>> sub DoSomeStuff ()
>>> Dim  objVar  as new foo
>>> for i as integer = 1 to 1000
>>> if i <= 50 then
>>> objvar.PerformsomeAction
>>> if  i=50 then objVar = Nothing
>>> else
>>> 'do something else except using ObjVar
>>> end if
>>> next
>>>
>>> End Sub
>>>
>>>
>>> Can be found in the best practices guidelines  from MS
>>>
>>> So i guess the reasson is that objVar can be released before it runs out
>>> of scope   ( before the method ends ) otherwise some writers at MS made
>>> some serious flaws in there documentation
>>>
>>>
>>>
>>> regards
>>>
>>> Michel Posseth [MCP]
>>>
>>>
>>>
>>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>>> news:OvsjH%23%23SGHA.4976@TK2MSFTNGP11.phx.gbl...
>>>> Michel,
>>>>
>>>> I answer your question in your other reply here. AFAIK does the GC not
>>>> start in the middle of a routine. That is AFAIK the only reason to
>>>> start the GC by hand. This with as exception that the action in your
>>>> sample is a kind of IO operation (including painting). Than I agree
>>>> that it can be a reason to do as you show, however, than I would set in
>>>> your sample the force of the GC. I did not see now that you did mean an
>>>> IO (or operation as that).
>>>>
>>>> Cor
>>>>
>>>>
>>>> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
>>>> news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>>>>>
>>>>> Well ......
>>>>>
>>>>>
>>>>> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples
>>>>> i see Nothing used even better i found some articles that state that
>>>>> in some situations it might even benefit the GC to use Nothing .
>>>>>
>>>>> Can you clean to good in your home ???
>>>>>
>>>>> and indeed as a VB6 progger i learned how to clean up my mess ....
>>>>> :-)
>>>>>
>>>>> in the example i showed in a  above post for instance it is in my
>>>>> opinion perfectly valid to use Nothing , as it might help in the
>>>>> release of the object before the method has finished
>>>>>
>>>>> regards
>>>>>
>>>>> Michel Posseth [MCP]
>>>>>
>>>>>
>>>>>
>>>>> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in
>>>>> bericht news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>>>>>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>>>>>> parameter that goes out of scope immediately after you are setting it
>>>>>> to
>>>>>> Nothing, so that's unnecessary.  Excessive setting objects to Nothing
>>>>>> is a
>>>>>> holdover from classic VB (and even there it's not required as much as
>>>>>> people
>>>>>> think).  Also, setting it Nothing in that method does not remove the
>>>>>> reference in the calling method - but even there it's not practical
>>>>>> unless
>>>>>> your calling method is so long that you're worried about encouraging
>>>>>> garbage
>>>>>> collection as soon as possible (even then, "Using" is a more direct
>>>>>> approach
>>>>>> if that's your concern).
>>>>>>
>>>>>> So, all you're left with in your function is:
>>>>>> if not (ob is nothing) then
>>>>>>  ob.close
>>>>>>
>>>>>> I think just using the Close methods on the objects directly from
>>>>>> your
>>>>>> calling method is cleaner.
>>>>>> --
>>>>>> David Anton
>>>>>> www.tangiblesoftwaresolutions.com
>>>>>> Instant C#: VB to C# converter
>>>>>> Instant VB: C# to VB converter
>>>>>> Instant C++: C# to C++ converter & VB to C++ converter
>>>>>> Instant J#: VB to J# converter
>>>>>>
>>>>>>
>>>>>>
>>>>>> "andreas" wrote:
>>>>>>
>>>>>>> Hi,
>>>>>>> I am working with the streams objects
>>>>>>> (filestream,streamwriter,streamreader)
>>>>>>> May I do or is it good programming to make a sub like :
>>>>>>>
>>>>>>> public sub Closestreams(ByRef ob as object)
>>>>>>> if not (ob is nothing) then
>>>>>>> ob.close
>>>>>>> ob = nothing
>>>>>>> end if
>>>>>>> end sub
>>>>>>>
>>>>>>> thanks for any response
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
20 Mar 2006 1:53 PM
Cor Ligthert [MVP]
Michael,

Before somebody thinks that I find it good practise this was in the message
from Michael
>
> and so it becomes in my opinion again good practice to release the object
> as soon as you are ready with it
>

What is the good practise in this, somebody who started with this fairytale

As long as there is more than enough resource and memory there is in my
opinion not any need to clean the older up.

It is not your desktop it is your garbage. You only need to put it at the
door when there is not enough room more and/or that you are unsure if there
is enough room until the next garbage is collected by the garbageman.

:-)

Cor

Show quoteHide quote
>>
>>
>> or am i missing something here ???
>>
>>
>> regards
>>
>> Michel
>>
>>
>>
>>
>>
>>
>>
>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>> news:e4FdmyBTGHA.1576@tk2msftngp13.phx.gbl...
>>> Michel,
>>>
>>> If the method from objvar.PerformsomeAction is calling the GC or by
>>> instance do a show of whatever, than it works in my opinion.
>>>
>>> :-)
>>>
>>> Cor
>>>
>>>
>>> "m.posseth" <mich***@nohausystems.nl> schreef in bericht
>>> news:eK$DBmBTGHA.5656@TK2MSFTNGP11.phx.gbl...
>>>> Cor ,
>>>>
>>>>
>>>>>AFAIK does the GC not start in the middle of a routine.
>>>>
>>>> well i wonder if it doesn`t ,,, ,, cause
>>>>
>>>>
>>>> this code
>>>>
>>>> sub DoSomeStuff ()
>>>> Dim  objVar  as new foo
>>>> for i as integer = 1 to 1000
>>>> if i <= 50 then
>>>> objvar.PerformsomeAction
>>>> if  i=50 then objVar = Nothing
>>>> else
>>>> 'do something else except using ObjVar
>>>> end if
>>>> next
>>>>
>>>> End Sub
>>>>
>>>>
>>>> Can be found in the best practices guidelines  from MS
>>>>
>>>> So i guess the reasson is that objVar can be released before it runs
>>>> out of scope   ( before the method ends ) otherwise some writers at MS
>>>> made some serious flaws in there documentation
>>>>
>>>>
>>>>
>>>> regards
>>>>
>>>> Michel Posseth [MCP]
>>>>
>>>>
>>>>
>>>> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
>>>> news:OvsjH%23%23SGHA.4976@TK2MSFTNGP11.phx.gbl...
>>>>> Michel,
>>>>>
>>>>> I answer your question in your other reply here. AFAIK does the GC not
>>>>> start in the middle of a routine. That is AFAIK the only reason to
>>>>> start the GC by hand. This with as exception that the action in your
>>>>> sample is a kind of IO operation (including painting). Than I agree
>>>>> that it can be a reason to do as you show, however, than I would set
>>>>> in your sample the force of the GC. I did not see now that you did
>>>>> mean an IO (or operation as that).
>>>>>
>>>>> Cor
>>>>>
>>>>>
>>>>> "Michel Posseth [MCP]" <M***@posseth.com> schreef in bericht
>>>>> news:ORrwjQ%23SGHA.4600@TK2MSFTNGP11.phx.gbl...
>>>>>>
>>>>>> Well ......
>>>>>>
>>>>>>
>>>>>> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples
>>>>>> i see Nothing used even better i found some articles that state that
>>>>>> in some situations it might even benefit the GC to use Nothing .
>>>>>>
>>>>>> Can you clean to good in your home ???
>>>>>>
>>>>>> and indeed as a VB6 progger i learned how to clean up my mess ....
>>>>>> :-)
>>>>>>
>>>>>> in the example i showed in a  above post for instance it is in my
>>>>>> opinion perfectly valid to use Nothing , as it might help in the
>>>>>> release of the object before the method has finished
>>>>>>
>>>>>> regards
>>>>>>
>>>>>> Michel Posseth [MCP]
>>>>>>
>>>>>>
>>>>>>
>>>>>> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in
>>>>>> bericht news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
>>>>>>> As Herfried says, it's pointless to set ob to Nothing - ob is just a
>>>>>>> parameter that goes out of scope immediately after you are setting
>>>>>>> it to
>>>>>>> Nothing, so that's unnecessary.  Excessive setting objects to
>>>>>>> Nothing is a
>>>>>>> holdover from classic VB (and even there it's not required as much
>>>>>>> as people
>>>>>>> think).  Also, setting it Nothing in that method does not remove the
>>>>>>> reference in the calling method - but even there it's not practical
>>>>>>> unless
>>>>>>> your calling method is so long that you're worried about encouraging
>>>>>>> garbage
>>>>>>> collection as soon as possible (even then, "Using" is a more direct
>>>>>>> approach
>>>>>>> if that's your concern).
>>>>>>>
>>>>>>> So, all you're left with in your function is:
>>>>>>> if not (ob is nothing) then
>>>>>>>  ob.close
>>>>>>>
>>>>>>> I think just using the Close methods on the objects directly from
>>>>>>> your
>>>>>>> calling method is cleaner.
>>>>>>> --
>>>>>>> David Anton
>>>>>>> www.tangiblesoftwaresolutions.com
>>>>>>> Instant C#: VB to C# converter
>>>>>>> Instant VB: C# to VB converter
>>>>>>> Instant C++: C# to C++ converter & VB to C++ converter
>>>>>>> Instant J#: VB to J# converter
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> "andreas" wrote:
>>>>>>>
>>>>>>>> Hi,
>>>>>>>> I am working with the streams objects
>>>>>>>> (filestream,streamwriter,streamreader)
>>>>>>>> May I do or is it good programming to make a sub like :
>>>>>>>>
>>>>>>>> public sub Closestreams(ByRef ob as object)
>>>>>>>> if not (ob is nothing) then
>>>>>>>> ob.close
>>>>>>>> ob = nothing
>>>>>>>> end if
>>>>>>>> end sub
>>>>>>>>
>>>>>>>> thanks for any response
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Author
20 Mar 2006 4:21 PM
David Anton
Yes - I said that if you're interested in encouraging garbage collection as
soon as possible then do that (or better yet, use 'Using').  Note that
setting to Nothing doesn't trigger garbage collection - it just makes it
eligible if no other references to the object exist.
However, to set a local or parameter to Nothing as the last line before it
goes out of scope or anywhere for a short-lived local is pointless - an
object is eligible for garbage collection when it goes out of scope, so why
set to Nothing immediately before going out of scope?  That is totally
pointless.  Also, the original poster assumed that this would affect the
argument passed in the calling routine - no way!  Setting a parameter to
Nothing doesn't set the original argument to Nothing.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



Show quoteHide quote
"Michel Posseth  [MCP]" wrote:

>
> Well ......
>
>
> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i see
> Nothing used even better i found some articles that state that in some
> situations it might even benefit the GC to use Nothing .
>
> Can you clean to good in your home ???
>
> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>
> in the example i showed in a  above post for instance it is in my opinion
> perfectly valid to use Nothing , as it might help in the release of the
> object before the method has finished
>
> regards
>
> Michel Posseth [MCP]
>
>
>
> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
> > As Herfried says, it's pointless to set ob to Nothing - ob is just a
> > parameter that goes out of scope immediately after you are setting it to
> > Nothing, so that's unnecessary.  Excessive setting objects to Nothing is a
> > holdover from classic VB (and even there it's not required as much as
> > people
> > think).  Also, setting it Nothing in that method does not remove the
> > reference in the calling method - but even there it's not practical unless
> > your calling method is so long that you're worried about encouraging
> > garbage
> > collection as soon as possible (even then, "Using" is a more direct
> > approach
> > if that's your concern).
> >
> > So, all you're left with in your function is:
> > if not (ob is nothing) then
> >  ob.close
> >
> > I think just using the Close methods on the objects directly from your
> > calling method is cleaner.
> > --
> > David Anton
> > www.tangiblesoftwaresolutions.com
> > Instant C#: VB to C# converter
> > Instant VB: C# to VB converter
> > Instant C++: C# to C++ converter & VB to C++ converter
> > Instant J#: VB to J# converter
> >
> >
> >
> > "andreas" wrote:
> >
> >> Hi,
> >> I am working with the streams objects
> >> (filestream,streamwriter,streamreader)
> >> May I do or is it good programming to make a sub like :
> >>
> >> public sub Closestreams(ByRef ob as object)
> >> if not (ob is nothing) then
> >> ob.close
> >> ob = nothing
> >> end if
> >> end sub
> >>
> >> thanks for any response
> >>
> >>
> >>
>
>
>
Author
20 Mar 2006 4:30 PM
David Anton
Another way to explain the futility of the original "ob = Nothing" is that
all you are doing anytime you set an object to Nothing is telling the
compiler that "I'm done with this reference".  Why would you need to state
this for a local or parameter as the last line of a short-lived routine? 
It's an example of coding by rote - not understanding the purpose of the
statement.
It was necessary I think during the pre-SP1 phase of VB4 - there was a bug
which prevented the cleanup of local objects when going out of scope, but
that's ancient history.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter



Show quoteHide quote
"Michel Posseth  [MCP]" wrote:

>
> Well ......
>
>
> It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i see
> Nothing used even better i found some articles that state that in some
> situations it might even benefit the GC to use Nothing .
>
> Can you clean to good in your home ???
>
> and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
>
> in the example i showed in a  above post for instance it is in my opinion
> perfectly valid to use Nothing , as it might help in the release of the
> object before the method has finished
>
> regards
>
> Michel Posseth [MCP]
>
>
>
> "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
> news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
> > As Herfried says, it's pointless to set ob to Nothing - ob is just a
> > parameter that goes out of scope immediately after you are setting it to
> > Nothing, so that's unnecessary.  Excessive setting objects to Nothing is a
> > holdover from classic VB (and even there it's not required as much as
> > people
> > think).  Also, setting it Nothing in that method does not remove the
> > reference in the calling method - but even there it's not practical unless
> > your calling method is so long that you're worried about encouraging
> > garbage
> > collection as soon as possible (even then, "Using" is a more direct
> > approach
> > if that's your concern).
> >
> > So, all you're left with in your function is:
> > if not (ob is nothing) then
> >  ob.close
> >
> > I think just using the Close methods on the objects directly from your
> > calling method is cleaner.
> > --
> > David Anton
> > www.tangiblesoftwaresolutions.com
> > Instant C#: VB to C# converter
> > Instant VB: C# to VB converter
> > Instant C++: C# to C++ converter & VB to C++ converter
> > Instant J#: VB to J# converter
> >
> >
> >
> > "andreas" wrote:
> >
> >> Hi,
> >> I am working with the streams objects
> >> (filestream,streamwriter,streamreader)
> >> May I do or is it good programming to make a sub like :
> >>
> >> public sub Closestreams(ByRef ob as object)
> >> if not (ob is nothing) then
> >> ob.close
> >> ob = nothing
> >> end if
> >> end sub
> >>
> >> thanks for any response
> >>
> >>
> >>
>
>
>
Author
21 Mar 2006 11:23 AM
andreas
Thanks David for your explanation, I hope  others people agree because  the
keywords close, dispose, finalyse, nothing were a litle confusing  to me
(and so are sometimes the articles)

Show quoteHide quote
"David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
news:8BDC984A-266E-482A-9646-56281968B6E9@microsoft.com...
> Another way to explain the futility of the original "ob = Nothing" is that
> all you are doing anytime you set an object to Nothing is telling the
> compiler that "I'm done with this reference".  Why would you need to state
> this for a local or parameter as the last line of a short-lived routine?
> It's an example of coding by rote - not understanding the purpose of the
> statement.
> It was necessary I think during the pre-SP1 phase of VB4 - there was a bug
> which prevented the cleanup of local objects when going out of scope, but
> that's ancient history.
> --
> David Anton
> www.tangiblesoftwaresolutions.com
> Instant C#: VB to C# converter
> Instant VB: C# to VB converter
> Instant C++: C# to C++ converter & VB to C++ converter
> Instant J#: VB to J# converter
>
>
>
> "Michel Posseth  [MCP]" wrote:
>
> >
> > Well ......
> >
> >
> > It also doesn`t hurt   , to use Nothing , and in a lot of MS examples i
see
> > Nothing used even better i found some articles that state that in some
> > situations it might even benefit the GC to use Nothing .
> >
> > Can you clean to good in your home ???
> >
> > and indeed as a VB6 progger i learned how to clean up my mess ....  :-)
> >
> > in the example i showed in a  above post for instance it is in my
opinion
> > perfectly valid to use Nothing , as it might help in the release of the
> > object before the method has finished
> >
> > regards
> >
> > Michel Posseth [MCP]
> >
> >
> >
> > "David Anton" <DavidAn***@discussions.microsoft.com> schreef in bericht
> > news:DB6AD6BB-706A-438D-BD63-2831FFF9A043@microsoft.com...
> > > As Herfried says, it's pointless to set ob to Nothing - ob is just a
> > > parameter that goes out of scope immediately after you are setting it
to
> > > Nothing, so that's unnecessary.  Excessive setting objects to Nothing
is a
> > > holdover from classic VB (and even there it's not required as much as
> > > people
> > > think).  Also, setting it Nothing in that method does not remove the
> > > reference in the calling method - but even there it's not practical
unless
> > > your calling method is so long that you're worried about encouraging
> > > garbage
> > > collection as soon as possible (even then, "Using" is a more direct
> > > approach
> > > if that's your concern).
> > >
> > > So, all you're left with in your function is:
> > > if not (ob is nothing) then
> > >  ob.close
> > >
> > > I think just using the Close methods on the objects directly from your
> > > calling method is cleaner.
> > > --
> > > David Anton
> > > www.tangiblesoftwaresolutions.com
> > > Instant C#: VB to C# converter
> > > Instant VB: C# to VB converter
> > > Instant C++: C# to C++ converter & VB to C++ converter
> > > Instant J#: VB to J# converter
> > >
> > >
> > >
> > > "andreas" wrote:
> > >
> > >> Hi,
> > >> I am working with the streams objects
> > >> (filestream,streamwriter,streamreader)
> > >> May I do or is it good programming to make a sub like :
> > >>
> > >> public sub Closestreams(ByRef ob as object)
> > >> if not (ob is nothing) then
> > >> ob.close
> > >> ob = nothing
> > >> end if
> > >> end sub
> > >>
> > >> thanks for any response
> > >>
> > >>
> > >>
> >
> >
> >
Author
19 Mar 2006 4:39 PM
AMercer
> I am working with the streams objects (filestream,streamwriter,streamreader)
> May I do or is it good programming to make a sub like :
>
> public sub Closestreams(ByRef ob as object)
> if not (ob is nothing) then
>  ob.close
>  ob = nothing
> end if
> end sub

I think I see what you are trying to do.  You want Closestreams to be a
general purpose closer / object destroyer for streams.  Because of late
binding, it would also work for other objects that provide close (eg
WebResponse).  Because of this and the variety of streams, you want ob as
object.  With ByRef ob, the sub will also destroy the reference to the
object, and that may be important if the reference is shared or is defined in
a module.

Well, I don't like this style of coding.  My bias is heavily toward early
binding.  If all you plan to do is ob.close and ob=nothing, then I would do
these operations inline.  In many cases, as others have pointed out, the
caller's ob may be about to go out of scope, so ob=nothing is not needed. 
When a local object variable is instantiated in a sub/function, I like to
destroy it in the same place in conformance with the documentation.  In this
way, months from now, someone can read the code, refer to the documentation,
and see that all is well.

So far, I don't see any good reason for what you proposed.  I could
speculate that you plan to have a similar general purpose opener to go with
your closer, and maybe you want both functions to do more than you have shown
so far (eg keep book on open resources, block if a resource is busy,
whatever).  But you haven't indicated anything like this.  So, as you posed
the question, I'm against it.
Author
19 Mar 2006 6:48 PM
andreas
Yes, you have seen what I was trying to do
And thanks for the advice, I suppose the other readers/writers agree.

"AMercer" <AMer***@discussions.microsoft.com> schreef in bericht
news:8F4ECED0-93BE-4BF8-B0CB-C210207B8505@microsoft.com...
> > I am working with the streams objects
(filestream,streamwriter,streamreader)
Show quoteHide quote
> > May I do or is it good programming to make a sub like :
> >
> > public sub Closestreams(ByRef ob as object)
> > if not (ob is nothing) then
> >  ob.close
> >  ob = nothing
> > end if
> > end sub
>
> I think I see what you are trying to do.  You want Closestreams to be a
> general purpose closer / object destroyer for streams.  Because of late
> binding, it would also work for other objects that provide close (eg
> WebResponse).  Because of this and the variety of streams, you want ob as
> object.  With ByRef ob, the sub will also destroy the reference to the
> object, and that may be important if the reference is shared or is defined
in
> a module.
>
> Well, I don't like this style of coding.  My bias is heavily toward early
> binding.  If all you plan to do is ob.close and ob=nothing, then I would
do
> these operations inline.  In many cases, as others have pointed out, the
> caller's ob may be about to go out of scope, so ob=nothing is not needed.
> When a local object variable is instantiated in a sub/function, I like to
> destroy it in the same place in conformance with the documentation.  In
this
> way, months from now, someone can read the code, refer to the
documentation,
> and see that all is well.
>
> So far, I don't see any good reason for what you proposed.  I could
> speculate that you plan to have a similar general purpose opener to go
with
> your closer, and maybe you want both functions to do more than you have
shown
> so far (eg keep book on open resources, block if a resource is busy,
> whatever).  But you haven't indicated anything like this.  So, as you
posed
> the question, I'm against it.
>