The private components field: where is used?

T

Theo Bebekis

Hi there

Every time I create a new Form in the VS 2003 IDE
i see the following entry

private System.ComponentModel.Container components = null;

which constantly remains null, when I add
controls (say a Button) or components (say Timer)
on the Form.

The protected Dispose() checks
if (components != null)
which always returns false.

Questions:
1. Where and when that private "components" field is used?
2. If it is always null
a. can I delete it safely?
b. why I don't see any compiler warning?

Also, that same declaration
private System.ComponentModel.Container components = null;
is done by the IDE when I create a new Component or CustomControl
file. But I don't intend to use it. Do I really need it?


Best regards
Theo
 
T

Theo Bebekis

Theo said:
Hi there

Every time I create a new Form in the VS 2003 IDE
i see the following entry

private System.ComponentModel.Container components = null;

which constantly remains null, when I add
controls (say a Button) or components (say Timer)
on the Form.

The protected Dispose() checks
if (components != null)
which always returns false.

Questions:
1. Where and when that private "components" field is used?
2. If it is always null
a. can I delete it safely?
b. why I don't see any compiler warning?

Also, that same declaration
private System.ComponentModel.Container components = null;
is done by the IDE when I create a new Component or CustomControl
file. But I don't intend to use it. Do I really need it?


Best regards
Theo

Using the "try and error" method I found that
for a Component descendant to be added into that
private "components" field, its parent form defines,
the class should provide a second constructor like the
following:

public ClassName()
{

}

public ClassName(IContainer container) : this()
{
container.Add(this);
}

PS. BTW, there are 3 Timer classes defined by the framework.
Two of them are Component descendants, but only the
System.Windows.Forms.Timer provides the appropriate
constructor
 

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