How to destroy an object and all references to it vb.net

M

Miguel Ribeiro

Hi,

The crux of my problem is that I want to destroy an object without using
the reference (by using a collection etc.) and have the reference
basically show 'nothing'

I have a MDI form with forms that are going to be displayed on it. The
layout is quite complex since I am going to try get the same
functionality as Outlook. What I need to know is this:

I have a module with variables containing all the forms e.g.
Public frmSetup as SetupForm
Public frmManagement as ManagementForm ETC...

I have made them public as since this application will function like
outlook with 3 windows (forms) side by side, each form has to
communicate with the next form.

I load (for example) the frmSetup in the Left Panel on the MDI form, I
then want to, at a later stage, load frmManagement into the left panel
on the MDI form and clear all the previous forms that were loaded into
the panel without using their variable name (frmSetup, frmManagement
etc.) I do this by running this bit of code:

Dim frmControl As Control
For Each frmControl In pnlPanel.Controls
If TypeOf frmControl Is Form Then
If frmControl.Name <> objForm.Name Then ' If
this is not the form I want to load in
the panel, then destroy it
CType(frmControl, Form).Close()
CType(frmControl, Form).Dispose()
frmControl = Nothing ' It doesn't seem to work
Application.DoEvents()

End If
End If
Next

It is at this point that I am stuck, when I set all the other forms to
nothing (via the collection) the variable that references the form is
not set to nothing. I thought if I deleted the underlying object (by
setting it to nothing? and Dispose etc) that it would clear all the
references to that object in memory.

Can anyone help

Thanks

Miguel Ribeiro
 
J

Jon Skeet [C# MVP]

Dim frmControl As Control
For Each frmControl In pnlPanel.Controls
If TypeOf frmControl Is Form Then
If frmControl.Name <> objForm.Name Then ' If
this is not the form I want to load in
the panel, then destroy it
CType(frmControl, Form).Close()
CType(frmControl, Form).Dispose()
frmControl = Nothing ' It doesn't seem to work
Application.DoEvents()

End If
End If
Next

It is at this point that I am stuck, when I set all the other forms to
nothing (via the collection) the variable that references the form is
not set to nothing. I thought if I deleted the underlying object (by
setting it to nothing? and Dispose etc) that it would clear all the
references to that object in memory.

You can never set an *object* to nothing - you can only set the value
of a *variable* to nothing. That in itself doesn't change any other
variables.

In this case, it sounds like you just want to add:

pnlPanel.Controls.Remove(frmControl)

However, you almost certainly (I haven't tried) can't do that within
the For Each loop. I suggest you build up a list of controls you want
to remove, then remove them all at the end.

Now, if you've still got other variables with references to the form,
you'll have to clear those up separately - there's no automatic way of
finding all the variables with a reference to an object and clearing
them.
 

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