bsp; Me.List.Item(Index), Customer)
End If
End Get
End Property
End Class
In your collection class, you'll need to override the Add method so that
instead of accepting a generic Object type, your method accepts only a
Customer object. You can then use the internal List property from the base
class to add the Customer object to the internal collection of generic
objects.
You also need to override the Item method so it returns a Customer object
instead of a generic Object data type. Notice the use of the CType
function. This function converts the data in the built-in List property
from a generic Object data type to a Customer data type.
Using inheritance, you certainly end up writing a lot less code to
implement a collection class when compared to the code you had to write in
Visual Basic 6.0. To use the collection class you created in Listing 4.3,
you could write code like that shown in Listing 4.4.
Listing 4.4 Collection Classes Are Very Easy to Use
Private Sub CustCollection()
Dim cust As Customer
Dim colcust As New Customers()
Dim strMsg As String
cust = New Customer()
cust.CustomerName = "Microsoft Corporation"
colcust.Add(cust)
cust = New Customer()
cust.CustomerName = "SAMS Publishing"
colcust.Add(cust)
strMsg = "Count = " & _
colcust.Count.ToString() & "<BR>"
For Each cust In colcust
strMsg &= cust.CustomerName & "<BR>"
Next
lblMsg.Text = strMsg
colcust.RemoveAt(0)
lblMsg.Text = strMsg & _
"After the removal = " & colcust.Count
End Sub
This code creates two new Customer objects, sets the CustomerName property
of each to a unique name, and then adds each one to the Customers
collection class. You can use the Count property from the base class to
determine the number of items in the collection, even though you did not
implement it in your Customers class. You can use the For Each iterator to
loop through each Customer object. You can also utilize the base class's
RemoveAt method to remove a specific item at a specified index in the
collection.