late binding

A

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
 
C

Cor Ligthert [MVP]

Andreas,

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

Cor
 
M

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]
 
C

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


Michel Posseth said:
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 said:
Andreas,

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

Cor
 
H

Herfried K. Wagner [MVP]

andreas said:
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...
 
A

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
 
C

Cor Ligthert [MVP]

Andreas,

What is in your opinion the advantage?

I do not see any, I see only disadvantages.

Cor
 
G

Guest

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
 
G

Guest

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.
 
A

andreas

Yes, you have seen what I was trying to do
And thanks for the advice, I suppose the other readers/writers agree.
 
M

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]
 
M

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]



Cor Ligthert said:
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 said:
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 said:
Andreas,

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

Cor

"andreas" <[email protected]> schreef in bericht
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
 
C

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


Michel Posseth said:
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 said:
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
 
M

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]



Cor Ligthert said:
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 said:
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 said:
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



:

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
 
C

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


m.posseth said:
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 said:
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 said:
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" <[email protected]> schreef in bericht
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



:

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
 
M

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







Cor Ligthert said:
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 said:
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 said:
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]" <[email protected]> schreef in bericht

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" <[email protected]> schreef in bericht
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



:

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
 
C

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
or am i missing something here ???


regards

Michel







Cor Ligthert said:
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 said:
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]



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]" <[email protected]> schreef in bericht

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" <[email protected]> schreef in
bericht 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



:

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
 
C

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
or am i missing something here ???


regards

Michel







Cor Ligthert said:
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" <[email protected]> schreef in bericht
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]



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]" <[email protected]> schreef in bericht

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" <[email protected]> schreef in
bericht 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



:

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
 
G

Guest

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



Michel Posseth said:
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 said:
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
 
G

Guest

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 said:
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 said:
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
 

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