switch (_order) {
case SortOrder.Ascending:
return this.Name.CompareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).CompareTo(this.Name);
default:
return this.Name.CompareTo(((Customer)obj).Name);
}
}
}
Okay, you now have a basic
Customer
domain object. It holds a customer's id, name, and rating
attributes and supplies overrides of the
Equals()
and
ToString()
methods. You'll also notice that it
implements the
System.IComparable
interface. Overall, it's a very simple class, but it gives you a nice,
self-contained object to put in your collection. And, it's likely to be a simplified representation of the
kinds of objects you'll be tossing into your own collections.
From here, you can construct a
Collection<T>
class and start filling it with instances of
Customer
objects. The following code demonstrates a few of the approaches you can take to placing items in your
collection:
[VB code]
Public Sub AddCollectionItemsTest()
Dim custColl As New Collection(Of Customer)()
custColl.Add(New Customer(1, "Sponge Bob"))
custColl.Add(New Customer(2, "Kim Possible"))
custColl.Add(New Customer(3, "Test Person"))
custColl.Insert(1, New Customer(4, "Inserted Person"))
custColl(3) = New Customer(9, "Fat Albert")
For Each cust As Customer In custColl
Console.Out.WriteLine(cust)
Next
End Sub
[C# Code]
public void AddCollectionItemsTest() {
Collection<Customer> custColl = new Collection<Customer>();
custColl.Add(new Customer(1, "Sponge Bob"));
custColl.Add(new Customer(2, "Kim Possible"));
custColl.Add(new Customer(3, "Test Person"));
custColl.Insert(1, new Customer(4, "Inserted Person"));
custColl[3] = new Customer(9, "Fat Albert");
foreach (Customer cust in custColl)
Console.Out.WriteLine(cust);
}
140
Chapter 8
11_559885 ch08.qxd 9/8/05 11:05 PM Page 140