473,385 Members | 2,274 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,385 software developers and data experts.

Disposing question

I have been looking at an example, and there is something I don't
inderstand.

Given: form1 calls form2
---------

Question: What is the use of having these lines in form2
--------------
using System.ComponentModel;
....
private System.ComponentModel.Container components = null;
....
protected override void Dispose(bool Disposing)
{
if(disposing)
{
components.Dispose();
}
base.Dispose(disposing);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #1
10 6461
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)

HTH,

Bill P.

On Fri, 01 Aug 2003 20:37:54 +0200, Patrick De Ridder <ng****@freeler.nl>
wrote:
I have been looking at an example, and there is something I don't
inderstand.

Given: form1 calls form2
---------

Question: What is the use of having these lines in form2
--------------
using System.ComponentModel;
...
private System.ComponentModel.Container components = null;
...
protected override void Dispose(bool Disposing)
{
if(disposing)
{
components.Dispose();
}
base.Dispose(disposing);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)

HTH,

Bill P.

On Fri, 01 Aug 2003 20:37:54 +0200, Patrick De Ridder <ng****@freeler.nl>
wrote:
I have been looking at an example, and there is something I don't
inderstand.

Given: form1 calls form2
---------

Question: What is the use of having these lines in form2
--------------
using System.ComponentModel;
...
private System.ComponentModel.Container components = null;
...
protected override void Dispose(bool Disposing)
{
if(disposing)
{
components.Dispose();
}
base.Dispose(disposing);
}
--
Patrick De Ridder
ng****@freeler.nl
--
Patrick De Ridder
ng****@freeler.nl


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3
On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
wrote:
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)

I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #4
On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
wrote:
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)

I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #5
Patrick De Ridder wrote:
|| On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
|| wrote:
||
||| They are required becuase System.Windows.Forms.Form inherits from
||| Component which requires Dispose to be overrideen for Garbage
||| Collection (in layman's terms...) ;)
|| I do understand that if you need a Dispose, you need to override and
|| redefine Dispose. But I don't understand why I need it because C#
|| looks after the garbage collecting automatically, unless I go outside
|| the .NET framework
|| --
|| Patrick De Ridder
|| ng****@freeler.nl

The components collection contains references for child controls added to the form (those who take a IContainer in their
constructor). Windows Froms will call Dispose on the components collection when a form is closed, effectively calling Dispose on all
child controls and as such releasing all unmanaged resources used by them.

Willy.
Nov 15 '05 #6
Patrick De Ridder wrote:
|| On Fri, 01 Aug 2003 12:23:52 -0700, Bill Priess <no*****@nospam.com>
|| wrote:
||
||| They are required becuase System.Windows.Forms.Form inherits from
||| Component which requires Dispose to be overrideen for Garbage
||| Collection (in layman's terms...) ;)
|| I do understand that if you need a Dispose, you need to override and
|| redefine Dispose. But I don't understand why I need it because C#
|| looks after the garbage collecting automatically, unless I go outside
|| the .NET framework
|| --
|| Patrick De Ridder
|| ng****@freeler.nl

The components collection contains references for child controls added to the form (those who take a IContainer in their
constructor). Windows Froms will call Dispose on the components collection when a form is closed, effectively calling Dispose on all
child controls and as such releasing all unmanaged resources used by them.

Willy.
Nov 15 '05 #7
Patrick De Ridder <ng****@freeler.nl> wrote:
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)
I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework


Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See

http://msdn.microsoft.com/library/de...l=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

which is also known as http://tinyurl.com/2k6e

for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #8
Patrick De Ridder <ng****@freeler.nl> wrote:
They are required becuase System.Windows.Forms.Form inherits from Component
which requires Dispose to be overrideen for Garbage Collection (in layman's
terms...) ;)
I do understand that if you need a Dispose, you need to override and
redefine Dispose. But I don't understand why I need it because C#
looks after the garbage collecting automatically, unless I go outside
the .NET framework


Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See

http://msdn.microsoft.com/library/de...l=/library/en-
us/cpgenref/html/cpconfinalizedispose.asp

which is also known as http://tinyurl.com/2k6e

for more information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9
On Sat, 2 Aug 2003 07:33:15 +0100, Jon Skeet <sk***@pobox.com> wrote:
Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See

Thank you.
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #10
On Sat, 2 Aug 2003 07:33:15 +0100, Jon Skeet <sk***@pobox.com> wrote:
Garbage collection doesn't happen deterministically - so resources
which need disposing of as soon as they're no longer needed require a
different approach. See

Thank you.
--
Patrick De Ridder
ng****@freeler.nl
Nov 15 '05 #11

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

Similar topics

15
by: Chris Capel | last post by:
I've heard a lot of talk on the forum about memory managment in .NET and disposing things, finalizing them, garbage collection, etc. My question is which managed types need to be disposed after...
0
by: Patrick De Ridder | last post by:
I have been looking at an example, and there is something I don't inderstand. Given: form1 calls form2 --------- Question: What is the use of having these lines in form2 --------------...
4
by: MC D | last post by:
Question: If I have a class which has a property which is a collection of widget objects (an arrayList of widgets), and both the containter class and the widget class implement the IDisposable...
4
by: Dakkar | last post by:
I have a program with windows forms and after execution of my program im making it invisible for working background progress and i have a dispose function like this protected override void...
13
by: MuZZy | last post by:
Hi, Just wanted to make sure i get it right: consider this class: // =========== START CODE ============= class Test { private SqlConnection con = null; public void Connect() { con = new...
5
by: keithv | last post by:
I came across the following code snippet in a project I inherited. Having 2 close() and 2 dispose() calls for essentially one stream seems to me to be way too much overkill. What is the proper...
0
by: Gman | last post by:
Hi, Objective: Draw a grid on a bitmap and set this as a panel's image. (Rather than draw the grid directly on the panel and redraw repeatedly in the paint event.) Problem: It works fine....
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
29
by: Jerry Spence1 | last post by:
I'm rather confused as to whether something should be disposed of, or not. What is the general rule? How can you be sure of doing the right thing? I've heard about disposing unmanaged resources but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
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...
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...

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.