Groups | Blog | Home
all groups > dotnet windows forms databinding > february 2008 >

dotnet windows forms databinding : prm = Nothing


Will
2/5/2008 7:06:01 AM
Just curious...

Given:
Dim cmd As New SqlClient.SqlCommand
...
Dim prm1 As New SqlClient.SqlParameter("@ID",'DOG')
cmd.Parameters.Add(prm1)

Does
cmd.Parameters.Clear
remove the prm1 from memory, or just from the cmd Parameters list?

Should I also perform a
prm1 = Nothing


TIA,
Jack Jackson
2/5/2008 5:37:48 PM
On Tue, 5 Feb 2008 07:06:01 -0800, Will
[quoted text, click to view]

Neither "removes prm1 from memory". cmd.Parameters.Clear() removes
any references to prm1 from the cmd.Parameters collection. Once that
has been done and when the variable prm1 no longer references the
parameter object, then the parameter object is available for garbage
collection and will be collected when the memory is needed.

Assuming that the variable prm1 is a local variable and will go out of
scope shortly after the code snippet you show, there is no advantage
to setting prm1 = Nothing since that effectively will happen as soon
Will
2/6/2008 4:13:01 AM
Thanks Jack.

I suppose when I said "remove prm1 from memory" I meant to "make available
for garbage collection."

--
-Will


[quoted text, click to view]
Jack Jackson
2/6/2008 7:18:08 AM
On Wed, 6 Feb 2008 04:13:01 -0800, Will
[quoted text, click to view]

In your example it probably isn't necessary to do clear the parameters
collection. Assuming that the reference to the SqlCommand object goes
out of scope in the routine, then it will be available for garbage
collection and that will make the parameter objects also available for
garbage collection.

You need to dispose of the command object. You may already be doing
that, but the best way is to use the Using syntax for the command
object:

Using cmd As SqlCommand = New SqlClient.SqlCommand
....
Will
2/6/2008 2:48:01 PM
Thanks for the continuing help. I have been explicitly closing and setting to
' = Nothing' my objects. I need to better explore memory and scope. Do you
know of any good articles you can point me to?

--
-Will


[quoted text, click to view]
Jack Jackson
2/6/2008 3:39:27 PM
On Wed, 6 Feb 2008 14:48:01 -0800, Will
[quoted text, click to view]

No I don't. Google is pretty good about finding articles, if you can
figure out search keywords that apply to what you want. Of course you
always have to be wary of anything you find - some of the information
out there is incorrect.

Setting a variable to Nothing is generally not necessary if the
variable has a short scope, like in a method. If the variable is at
the class level and the class will stay alive for a while, then it
AddThis Social Bookmark Button