Re: How to clear controls from a form - Help Needed



Siv,

You could load a datatable from the datareader and return the datatable. But
you can't return a copy of the datareader.

Kerry Moorman


"Siv" wrote:

Cor,
This conversation you are having with Jack raises a question about disposal
which I have never been fully happy about. I often create functions that
return say a datareader to the caller. The purpose being to have a nice
little black box that does the job of getting data from a database based on
the paramters you send it.

My function would look something like this:

Private Function GetData(ByVal ID as integer) as SQLDatareader
Dim DR as SQLDataReader=nothing
Dim Cmd as SQLCommand=Nothing
Dim Cn as SQLConnection= Nothing
Dim strSQL as string = ""

Try

strSQl = "Select * From SomeTable where fkSomeForeignKey=" &
ID.Tostring & " ORDER BY SomeField;"
Cn = New SqlConnection(ConnString)
Cn.Open()

Cmd = New SqlCommand(strSQL, Cn)
Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection)

Return Dr

Catch ex As Exception

PEH("GetAllRecords", "Documents Class", ex.Message)
Return Dr

End Try
End Function

What I would really like to do is have a Finally block that disposes od Dr,
but if I do that I will get an error when I try and read the returned
DataReade in the calling routine as the Finally call will remove the version
I have sent to the caller before it gets it.

Is just leaving the function like that OK, or is there some way you can send
a copy of the datareader back to the caller and dispose the one in the body
of the function without causing problems?


Siv

--
Martley, Near Worcester, UK


"Cor Ligthert [MVP]" wrote:

Jack,

Absolute true, I had it first in my message but was afraid that it would
create to much confusion (and discussion). I normally try to write that part
exactly like you do, and it is very important in the case there are
unmanaged resources used. Which is AFAIK by instance not the fact "in"
controls. (but can be used in objects that controls are using).

I have seen it so much written in the context like I did now, so I did that
a bit like that to avoid a discussion. Normally I don't use that because it
is for me really a minor aspect to take action to clear 100bytes memory a
little bit quicker while I have 1Gb not used.

Cor

"Jack Jackson" <jjackson@xxxxxxxxxxxxxxxx> schreef in bericht
news:6trf94l79352peqi1qep0et83j3ut6qdev@xxxxxxxxxx
Cor,

I'm sure it is useless to argue with you, but it is misleading to say
that the purpose of Dispose is to tell the GC that the object " 'can
be' ready to clear up".

The purpose if Dispose is to release unmanaged resources. For
Controls, that would mostly be the Windows handle for the associated
Windows control. If you fail to call Dispose for a Control that has
allocated a Windows handle, then the handle might no be freed for a
very long time, and if you do this enough times you might run out of
Windows handles.

On Tue, 5 Aug 2008 06:47:35 +0200, "Cor Ligthert[MVP]"
<notmyfirstname@xxxxxxxxx> wrote:

Siv,

Disposing is not a removing method or whateverf, it is just telling the GC
that it "can be" ready to clear up, like the GC does that automaticly when
it is really needed. Dispose is meant to put it at the garbage. As
anology,
some people are the whole day busy with cleaning up there desktop (putting
it in the trash bin, which still will not be direct emptied), others do
that
once a day or/and some when that is needed.

As it is about forms, you can see in your designer part of the class
(versions before 2005 normal in the class) the implementing of IDisposable
of a form.

As long as an object is referenced by something (like on a form with
controls.add) it will not be cleared as long as that has a reference,
whatever other method you call (including dispose), mostly is that by
clear
them all in one time or by removing them one by one.

Cor


"Siv" <Siv@xxxxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:EBBE4C00-2842-475E-97E5-01F9D7491B53@xxxxxxxxxxxxxxxx
Stephany,

Thanks for clarifying, clearly I thought that to dispose of a control
would
by inference remove it from the tpMain collection of controls. It's the
old
story "never assume".

Thanks for your help.

Siv
--
Martley, Near Worcester, UK


"Stephany Young" wrote:

Your problem was that, although you were 'disposing' the control, you
were
not removing it from tpMain's collection of controls. Therefore you
end
up
with 'phantom' controls in tpMain's control collection.

Others might argue to the contrary, but I believe that the Dispose
method
of
a CheckBox control does nothing special, and it certainly will not
remove
itself from it's parent collection.

Therefore the code can be further simplified to:

For _i = tpMain.Controls.Count - 1 To 0 Step -1
Dim _ctrl = tpMain.Controls(_i)
If TypeOf (_ctrl) Is CheckBox AndAlso _ctrl.Name.StartsWith("chkS")
Then
tpMain.Controls.RemoveAt(_i)
Next

To save you asking, the reverse loop is to ensure that iterating on the
control collection is still viable even when one or more of the
iteration
actually removes an element.



"Siv" <Siv@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3A906198-C470-43E1-9A2D-3A3E2CDF834E@xxxxxxxxxxxxxxxx
Stephany,
Thanks for your reply. Can you explain why the additional command

tpMain.Controls.RemoveAt(_i)

will do the trick where just doing the dispose on its own does not?

I appreciate you gave me the answer, but I try to learn from my
questions
here. Don't worry if you are too busy to reply, but if you do have
time
I
would appreciate it.

Siv
--
Martley, Near Worcester, UK


"Stephany Young" wrote:

For _i = tpMain.Controls.Count - 1 To 0 Step -1
Dim _ctrl = tpMain.Controls(_i)
If TypeOf (_ctrl) Is CheckBox AndAlso
_ctrl.Name.StartsWith("chkS")
Then
tpMain.Controls.RemoveAt(_i)
_ctrl.Dispose()
End If
Next


"Siv" <Siv@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:ECC5102D-8DAC-4D39-B504-0F4E8BD8A6F8@xxxxxxxxxxxxxxxx
I have a form that I programmatically generate some check boxes and
labels
on.
Later on when I want to draw the form with different data I want
to
clear
the previously created items and then put some new ones on.

In my code I am doing the following:

For Each ctrl In tpMain.Controls
If TypeOf (ctrl) Is CheckBox Then
If ctrl.Name.StartsWith("chkS") Then
ctrl.Visible = False
ctrl.Dispose()
End If
End If
End if
Next

What I am finding however is that in some cases controls that I
know
have
been affected by the above loop are staying on the form and not
being
made
invisible and then being disposed.

Is there a sure fire method of removing the controls rather than
the
dispose
method which presumably is not always removing the control for
whatever
reason??

Siv
--
Martley, Near Worcester, UK







.



Relevant Pages

  • Re: How to clear controls from a form - Help Needed
    ... At the moment I do return the datareader, but I have to leave the Datareader ... (but can be used in objects that controls are using). ... >>> The purpose if Dispose is to release unmanaged resources. ... that would mostly be the Windows handle for the ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to clear controls from a form - Help Needed
    ... Why would you cleanup a datareader itself, which is created in a method. ... don't forget before that to call the method close or dispose to release things as the connection in the connectionpool etc. in the code you have created. ... (but can be used in objects that controls are using). ... that would mostly be the Windows handle for the associated ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to clear controls from a form - Help Needed
    ... implicitly dispose of the Datareader (doing so would stop the function doing ... don't forget before that to call the method close or dispose (does ... (but can be used in objects that controls are using). ... If TypeOf (_ctrl) Is CheckBox AndAlso ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to clear controls from a form - Help Needed
    ... At the moment I do return the datareader, but I have to leave the Datareader ... Dim DR as SQLDataReader=nothing ... a copy of the datareader back to the caller and dispose the one in the body ... (but can be used in objects that controls are using). ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to clear controls from a form - Help Needed
    ... My biggest worry with this was that over a day's usage if I didn't dispose ... because the system had run out of memory to keep tabs on all the controls on ... that would mostly be the Windows handle for the associated ... Disposing is not a removing method or whateverf, it is just telling the GC ...
    (microsoft.public.dotnet.languages.vb)

Loading