Thanks Bill,
2 further quesitons
1..
Do form object go out of scope when you call Me.Close on the form? If not
how do you make a form go out of scope?
2..
How does this work? I have some data access objects that I declare in the
declaration section of a sub then .Dispose of but they are still accessable.
Note in particular the DataAdapter, Dataset, Command and Parameter objects
in the following code. I would have thought that if I free the memory for
these objects with the .Dispose method it would throw some sort of bounds
exception if I tried to use them again. This code works without errors
everytime I use it. Why doesn't Dispose clear the objects?
Private Sub GetC()
Dim sqlStr As String
Dim C As Double
Dim con As New OleDb.OleDbConnection()
Dim cmd As New OleDb.OleDbCommand()
Dim da As New OleDb.OleDbDataAdapter()
Dim ds As New DataSet()
Dim parm As New OleDb.OleDbParameter()
C = 0
Try
sqlStr = "SELECT [C_Value], [ID] FROM
WHERE ([ID]= ? )"
con.ConnectionString = ADB_strCon
cmd.Connection = con
cmd.CommandText = sqlStr
parm.Direction = ParameterDirection.Input
parm.DbType = DbType.String
parm.ParameterName = "?"
parm.Value = txtProvider1.Text
parm.Size = Len(txtProvider1.Text)
cmd.Parameters.Add(parm)
da.SelectCommand = cmd
da.Fill(ds, "C_Value")
C = C + ds.Tables("C_Value").Rows(0)(0)
ds.Clear()
ds.Dispose()
da.Dispose()
cmd.Parameters.Clear()
cmd.Dispose()
cmd.Connection = con
cmd.CommandText = sqlStr
parm.Value = txtProvider2.Text
parm.Size = Len(txtProvider2.Text)
cmd.Parameters.Add(parm)
da.SelectCommand = cmd
da.Fill(ds, "C_Value")
C = C + ds.Tables("C_Value").Rows(0)(0)
ds.Clear()
ds.Dispose()
da.Dispose()
cmd.Parameters.Clear()
cmd.Dispose()
cmd.Connection = con
cmd.CommandText = sqlStr
parm.Value = txtProvider3.Text
parm.Size = Len(txtProvider3.Text)
cmd.Parameters.Add(parm)
da.SelectCommand = cmd
da.Fill(ds, "C_Value")
C = C + ds.Tables("C_Value").Rows(0)(0)
ds.Clear()
ds.Dispose()
da.Dispose()
cmd.Parameters.Clear()
cmd.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Bill McCarthy said:
Hi Michael,
If you can explicitly call Dispose, it is always best to do this as this
provides timely release of resources the object may be holding onto. If you
don't call Dispose, the GC will eventually collect them for you once they go out
of scope, but that may take some time, and system resources could get low if any
of the objects waiting to be collected are holding onto resources, such as
bitmaps, brushes, fonts, or just large objects that take up large amounts of
memory.
So, the general rule is if you can safely call IDisposable.Dispose on the
object, then do so.
HTH's
Bill.
Michael Hart said:
I have a form that creates new objects in all sorts of fashion using vb.net.
Some are created when the form opens with
Private varName as new Object()
just near the form class
In some subs I have
Sub Internal1()
Dim myVar as new Object()
End Sub
in the declaration section
and also sometimes I have the constructer separate to the declaration
Sub Internal2()
Dim newVar as Object
newVar = new Object(parm1, parm2)
End Sub
Do I need to explicitly dispose of these objects or will the garbage
collector [if there is one] take care of it when they go out of scope?
When I do a Me.Close on a form does it invoke the garbage collector on the
form?