The System.Collections Namespace

IOTA^_^    

ASP.NET Developer's JumpStart
By Paul D. Sheriff, Ken Getz
Table of Contents
Chapter 4.  Overview of .NET Framework Classes


The System.Collections Namespace

Use the System.Collections namespace when you need to create a data structure that can hold a group of similar objects. The System.Collections namespace contains all the classes and interfaces needed to define collections of objects. Some of the classes that you might use most often are listed in Table 4.4.

Table 4.4. System.Collections Classes
Class Description
ArrayList An ArrayList is similar to an array, but the ArrayList class allows you to add items without managing the size of the list yourself (much like a VB Collection object).
CollectionBase This class is the base for a strongly typed collection, which is critical for creating collection classes.
DictionaryBase This class is the base for a strongly typed collection utilizing associated keys and values. This is similar to the Dictionary object found in VBScript and used by many ASP developers.
SortedList This class represents a collection of keys and values that you insert into the list. A SortedList object is always automatically sorted by the key value. You can access the values with either the key value or an index number.

A common use of the System.Collections namespace is to create strongly typed collections. Consider the following scenario:

Assume you create a class called Customer. The Customer class has a number of properties, methods, and events. You now want to create several Customer objects within one structure so you can iterate through the collection and print each customer to the printer. You could just create a variable of type Array, ArrayList, or SortedList, but these standard collections can hold any type of object. In other words, you could add a Customer object as the first item in the collection, but you could then add an Employee object as the second item, a String object as the third, and so on. If you try to iterate over this collection and apply the Update method, it might work on the Customer and Employee objects, but when your loop attempts to call the Update method of a String object, it will certainly fail.

To get around this sticky situation, you can create your own class that looks like a generic collection object but restricts its members to only one data type. This new class will include the standard methods of the collection base type, such as the Add and Remove methods, as well as an Item property, but your class's Add method will only allow you to add Customer objects.

In Visual Basic 6.0, you could provide this functionality, and you generally started by creating an empty class and then creating an Add method, a Remove method, an Item property, a Count property, and so on. This is easier in .NET, thanks to the base classes in the System.Collections namespace.

A System.Collections Example

Assume that you need to create a Customer object to hold values from a Customer table in your database. The structure of the Customer object is very simple, containing just a CompanyName property. Imagine that you want to create a collection class named Customers (note the letter s on the end) to hold a set of Customer objects. You first need to create the Customer class as shown in the following code snippet:

 Public Class Customer     Public CustomerName As String End Class 

Next, you create your new Customers collection class. Your class must inherit from the System.Collections.CollectionBase class. (This base class gives you all the functionality of a normal collection but allows you to override any of the base methods, such as Add and Item.) Listing 4.3 provides the new class and its overridden Add and Item members:

Listing 4.3 Building Collection Classes Is Easy with the .NET Framework Class CollectionBase
 Public Class Customers     Inherits System.Collections.CollectionBase     Public Sub Add(ByVal cust As Customer)         Me.List.Add(cust)     End Sub     Public ReadOnly Property Item( _      ByVal Index As Integer) As Customer         Get             If Index > Count - 1 Or Index < 0 Then                 ' Return error message             Else                 Return CType( _                  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.


    IOTA^_^    
    Top


    ASP. NET Developer's JumpStart
    ASP.NET Developers JumpStart
    ISBN: 0672323575
    EAN: 2147483647
    Year: 2002
    Pages: 234

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