GENERICS

Chapter 26 explains how you can build and use generic classes to perform similar actions for objects of various types. For example, you could build a Tree class that can build a tree of any specific kind of object. Your program could then make a tree of Employees, a tree of Customers, a tree of Punchlines, or even a tree of trees. Visual Basic comes with a useful assortment of prebuilt generic collection classes.

The System.Collections.Generic namespace provides several generic collection classes that you can use to build strongly typed collections. These collections work with a specific data type that you supply in a variable’s declaration. For example, the following code makes a List that holds strings:

Imports System.Collections.Generic
 
...
Dim places As New List(Of String)
places.Add("Chicago")

The places object’s methods are strongly typed and work only with strings, so they provide extra error protection that a less specialized collection doesn’t provide. To take advantage of this extra protection, you should use generic collections whenever possible.

You cannot directly modify a generic collection class, but you can add extension methods to it. For example, the following code adds an AddPerson method to the generic List(Of Person) class. This method takes as parameters a first and last name, uses those values to make a Person object, and adds it to the list.

Module PersonListExtensions
    <Extension()>
    Public Sub AddPerson(person_list As List(Of Person),
     first_name As String, last_name As String)
        Dim per As New Person() With _
            {.FirstName = first_name, .LastName = last_name}
        person_list.Add(per)
    End Sub
End Module

For more information on extension methods, see the section “Extension Methods” in Chapter 16, “Subroutines and Functions.”

In addition to adding extension methods to a generic class, you can also derive an enhanced collection from a generic class. For example, the following code defines an EmployeeList class that inherits from the generic List(Of Employee). It then adds an overloaded version of the Add method that takes first and last names as parameters.

Imports System.Collections.Generic
 
Public Class EmployeeList
    Inherits List(Of Employee)
 
    Public Overloads Sub Add(
     first_name As String, last_name As String)
        Dim emp As New Employee(first_name, last_name)
        MyBase.Add(emp)
    End Sub
End Class

NO OVERLOADS ALLOWED
Note that extension methods cannot overload a class’s methods. If you want multiple versions of the Add method as in this example, you need to use a derived class.

The following table lists some of the most useful collection classes defined by the System.Collections.Generic namespace.

COLLECTION PURPOSE
Comparer Compares two objects of the specific type and returns -1, 0, or 1 to indicate whether the first is less than, equal to, or greater than the second
Dictionary A strongly typed dictionary
LinkedList A strongly typed linked list
LinkedListNode A strongly typed node in a linked list
List A strongly typed list
Queue A strongly typed queue
SortedDictionary A strongly typed sorted dictionary
SortedList A strongly typed sorted list
Stack A strongly typed stack

Example program GenericStringList, which is available for download on the book’s website, demonstrates a generic List(Of String). Example program GenericEmployeeList, which is also available for download, derives a strongly typed EmployeeList class from a generic List(Of Employee).

For more information on generics (including instructions for writing your own generic classes), see Chapter 26.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.103.74