Containers


When working with components, you may find it useful to store them in a container. As explained earlier, a container holds a group of components. Its main advantage is that it allows a group of components to be managed collectively. For example, you can dispose of all of the components in a container by calling Dispose( ) on the container. In general, containers make working with multiple components much easier.

To create a container, you will create an object of the Container class, which is defined in the System.ComponentModel namespace. It has this constructor:

 public Container( )

This creates an empty container.

Once you have created a Container object, you add components to it by calling Add( ). It has the two forms shown here:

 public virtual void Add(IComponent comp) public virtual void Add(IComponent comp, string compName)

The first form adds comp to the container. The second form adds comp to the container and gives it the name specified by compName. The name must be unique, and case differences are ignored. An ArgumentException is thrown if you attempt to use a name that is already in the container. This name is obtainable through the Site property of the component.

To remove a component from a container, use Remove( ), as shown here:

 public virtual void Remove(IComponent comp)

This method is assumed to succeed because it either removes comp, or comp was not in the container in the first place. In either case, comp is not in the container after the call to Remove( ).

Container implements Dispose( ). When Dispose( ) is called on a container, all components stored in the container will have their Dispose( ) methods called. Thus, you can dispose of all the components in a container with a single call to Dispose( ).

Container defines one property called Components, which is defined like this:

 public virtual ComponentCollection Components{ get; }

This property obtains a collection of the components that are stored in the invoking container.

Recall that Component defines properties called Container and Site, which will be included in all derived components. When a component is stored in a container, the object’s Container and Site properties are automatically set. The Container property refers to the container in which the component is stored. Site links a component and a container, encapsulating information about the component, including its name, the container, and whether it is in design mode. The Site property returns an ISite reference. This interface defines the following properties:

Property

Description

IComponent Component { get; }

Obtains a reference to the component

IContainer Container { get; }

Obtains a reference to the container

bool DesignMode { get; }

Is true if the component is being used by a design tool

string Name { get; set; }

Obtains or sets the name of the component

You can use these properties to obtain information about the runtime container or component at runtime.

Demonstrating a Container

The following program uses a container to store two CipherComp components. The first is added to the container without specifying a name. The second is given the name “Second Component”. Next, operations occur on both components. Then the name of the second component is displayed via its Site property. Finally, Dispose( ) is called on the container, releasing both components. (A using statement could have been employed here, but for the sake of illustration, Dispose( ) is called explicitly.)

 // Demonstrate a component container. using System; using System.ComponentModel; using CipherLib; // import CipherComp's namespace class UseContainer {   public static void Main(string[] args) {     string str = "Using containers.";     Container cont = new Container();     CipherComp cc = new CipherComp();     CipherComp cc2 = new CipherComp();     cont.Add(cc);     cont.Add(cc2, "Second Component");     Console.WriteLine("First message: " +  str);     str = cc.Encode(str);     Console.WriteLine("First message encoded: " +                       str);     str = cc.Decode(str);     Console.WriteLine("First message decoded: " +                       str);     str = "one, two, three";     Console.WriteLine("Second message: " +  str);     str = cc2.Encode(str);     Console.WriteLine("Second message encoded: " +                       str);     str = cc2.Decode(str);     Console.WriteLine("Second message decoded: " +                       str);     Console.WriteLine("\ncc2's name: " + cc2.Site.Name);     Console.WriteLine();     // Release both components.     cont.Dispose();   } }

The output from the program is shown here:

 First message: Using containers. First message encoded: Vtjoh!dpoubjofst/ First message decoded: Using containers. Second message: one, two, three Second message encoded: pof-!uxp-!uisff Second message decoded: one, two, three cc2's name: Second Component Dispose(True) for component 1 Closing file for component 1 Dispose(True) for component 0 Closing file for component 0

As you can see, by calling Dispose( ) on the container, all components stored in that container are disposed. This is a major benefit when working with many components or component instances.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net