|
|
|
Creating Custom Collections
Creating custom collections is a relatively
simple, if not
Implementing the CollectionBase ClassTo 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
Listing 5.11. Address Collection
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;
}
}
}
|
|
|
|