473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
Jan 12 '06 #1
1 3601
Miguel Ribeiro <ni****@noemail.noemail> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Ilsche | last post by:
Hi, how do I "explicitly destroy" an Object in PHP5 to make sure the destructor is called an the object destroyed? unset is not an option because there are multiple variables containing the...
7
by: KraftDiner | last post by:
if I create an object like... obj = None .... obj = anObject() can obj set itself to none in some method of the class?
2
by: Sarfraz Hooda | last post by:
Hi, I have created an array of Objects in a collection. I was wondering is there a way to destroy the array to free up the space in the memory ? or they are automatically destroyed and garbagge...
7
by: Claire | last post by:
How do I make sure Ive disposed of my objects please? Ive added interface IDispose to a base class. Ive several sub classes inheriting from this base class. I store one or other of these...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
16
by: LP | last post by:
Hello, I am trying to use .NET with Excel. I installed Office 2003 and selected ..NET programming suport option, so it installed all those PIA, as MS sugests. But I can not find a way to destroy...
4
by: Curious | last post by:
Hi, I have a timer object that's launched as below: mTimer = new System.Threading.Timer (new TimerCallBack(SubscribeTrade), null, 15000, 15000); void SubscribeTrade (object state) {...
4
by: Juergen-Bernhard Adler | last post by:
Hello, pretend some noob has (in a fake-static class) provided the following method public static kill_object($obj) { if (!is_object($obj)) return false;
6
by: itsraghz | last post by:
Dear All, I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.