System.ComponentModel.IContainer

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Why do some forms on my project have the components variable as

System.ComponentModel.IContainer components = null;

Yet others have

System.ComponentModel.Container components = null;

What is the difference of this variable being a reference to the Container
class as opposed to the Interface?

Thank you
 
System.ComponentModel.IContainer components = null;
Yet others have

System.ComponentModel.Container components = null;

What is the difference of this variable being a reference to the
Container class as opposed to the Interface?
The first declaration can contain a reference to any object that
implements the IContainer interface. The second can point only to a
System.ComponentModel.Container class, or a class that inherits from
System.ComponentModel.Container.

You can easily cast them:

System.ComponentModel.Component ob = new Component();
System.ComponentModel.IComponent inter;
inter = (System.ComponentModel.IComponent) ob;
ob = (System.ComponentModel.Component) inter;

The cast from Component to IComponent should always succeed. Casting from
IComponent to Component success only when the former is a reference to a
Component, not to another class that implements the IComponent interface.

Here's some sample code to demonstrate the difference:

class NotAComponent : IComponent
{
public ISite Site { get { return null; } set { Site = value; } }
public event EventHandler Disposed;
public void Dispose() {}
};


System.ComponentModel.Component ob = new Component();
System.ComponentModel.IComponent inter;
// OK, Component implements IComponent
inter = (System.ComponentModel.IComponent)ob;
// OK, IComponent contains a Component
ob = (System.ComponentModel.Component)inter;
// OK, NotAComponent implements IComponent
inter = new Urg();
// Exception, NotAComponent does not contain a Component
ob = (System.ComponentModel.Component)inter;


Greetings,
Wessel
 
Thank you for your answer, but I want to know why the VS2003 IDE sets this to
Containerfor some forms and IContainer for some others.

I notice that the ones that have an image list have the compenents variable
as IContainer, but the ones without image lists have Container.

In terms of windows forms, what is the difference?

Thank you.
 
Thank you for your answer, but I want to know why the VS2003 IDE sets
this to
Containerfor some forms and IContainer for some others.
I have no clue about that, sorry.

Greetings,
Wessel
 

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

Back
Top