Team LiB
Previous Section Next Section

Creating Custom Collections

Creating custom collections is a relatively simple, if not tedious, task. To create a Collection, simply create a class that implements the ICollection interface. However, to implement the ICollection interface, you must also implement the IEnumerable interface. If the custom collection that you plan to implement does not use a list of some sort that already implements this interface, you will have to implement it yourself.

Implementing the CollectionBase Class

To make the task of creating a custom collection easier, the .NET Framework provides a base class called CollectionBase. The CollectionBase class is an abstract base class for providing strongly typed collections. Instead of writing your own base class, it is easier and recommended that you extend this class.

Creating a AddressList Collection

Using the knowledge you gained in the previous sections, you will now create a custom collection of addresses. To do this, we will create a class that descends from the CollectionBase class. You will override a few methods so that the collection returns a concrete type of Address, instead of the Object type returned by all out-of-the-box collections. Listing 5.11 demonstrates how to extend the CollectionBase class to provide a strongly typed collection of addresses.

Listing 5.11. AddressCollection
using System;

namespace AddressCollection
{
  public class CustomAddress
  {
    protected string m_id;
    protected string m_city;
    protected string m_country;
    protected string m_postalCode;
    protected string m_state;
    protected string m_streetAddress1;
    protected string m_streetAddress2;
    protected string m_suite;

    public void Copy(CustomAddress address)
    {
      if(null != address)
      {
        m_id                                             = address.m_id;
        m_city                                           = address.m_city;
        m_country                                        = address.m_country;
        m_postalCode                                     = address.m_postalCode;
        m_state                                          = address.m_state;
        m_streetAddress1                                 = address.m_streetAddress1;
        m_streetAddress2                                 = address.m_streetAddress2;
      }
    }

    public CustomAddress()
    {
    }
  }

  /// <summary>
  /// Address:
  /// </summary>
  public class Address : CustomAddress
  {

    public Address() : base()
    {
    }

    public string Country {get { return m_country; } set { m_country = value; } }
    public string Id { get { return m_id; } set { m_id = value; } }
    public string PostalCode
       { get { return m_postalCode; } set { m_postalCode = value; } }
    public string State { get { return m_state; } set { m_state = value; } }
    public string City { get { return m_city; } set { m_city = value; } }
    public string StreetAddress1
       { get { return m_streetAddress1; } set { m_streetAddress1 = value; } }
    public string StreetAddress2
       { get { return m_streetAddress2; } set { m_streetAddress2 = value; } }
  }

  public class AddressCollection : System.Collections.CollectionBase
  {
    public AddressCollection() : base()
    {
    }

    public virtual int Add(Address value)
    {
      return base.InnerList.Add(value);
    }

    public Address GetItem(int index)
    {
      return (Address) List[index];
    }
    public void SetItem(int index, Address value)
    {
      List[index] = value;
    }
  }
}

    Team LiB
    Previous Section Next Section