473,831 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Contro ls
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(frmContro l, Form).Close()
CType(frmContro l, Form).Dispose()
frmControl = Nothing ' It doesn't seem to work
Application.DoE vents()

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 3662
Miguel Ribeiro <ni****@noemail .noemail> wrote:

<snip>
Dim frmControl As Control
For Each frmControl In pnlPanel.Contro ls
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(frmContro l, Form).Close()
CType(frmContro l, Form).Dispose()
frmControl = Nothing ' It doesn't seem to work
Application.DoE vents()

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.Contro ls.Remove(frmCo ntrol)

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.co m>
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
15271
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 object handle. Waiting for the end of the script is aswell a bad option because the order in which the destructores are called does matter.
7
1970
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
7067
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 collected by .Net framework? Sarfraz
7
6327
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 subclasses in a variable and call dispose(), but the Dispose() method of the base class is called rather than in the sub class example class baseclass : IDisposable
8
1952
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 anymore until I kill that process. Obviously, this is not acceptable. Looking back, I do not destroy any objects in my form. Would that be the reasn why the application breaks down?
16
4559
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 Excel process, it still hangs in the taks manager' Processes as running. I am trying very simple code (see below), but Excel wont go away, I tried just about anything I could find on MSDN or by Google. Even calling WIN32 API to destroy Excel. But...
4
2947
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) { DateTime now = DateTime.Now;
4
2033
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
16921
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 executed conditionally through Program A based on the commands it gets from the user. Let's say, we have two inputs, "start" and "stop" to drive the Program B. I have thought of various alternatives. (1) Using Thread to control the execution...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10778
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10496
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10538
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10210
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.