Generics


Chapter 19 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. While you can build classes such as these yourself, Visual Basic comes with a useful assortment of pre-built 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 or strongly typed collections derived from the CollectionBase class whenever possible.

When you derive a strongly typed collection from the CollectionBase class, you can add extra convenience functions to it. For example, an EmployeeCollection class could include an overloaded version of the Add method that accepts first and last names as parameters, makes a new Employee object, and adds it to the collection.

You cannot directly modify a generic collection class, but you can derive an enhanced collection from one. For example, the following code defines an EmployeeCollection class that inherits from the generic Collection(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(ByVal first_name As String, ByVal last_name As String)         Dim emp As New Employee(first_name, last_name)         MyBase.Add(emp)     End Sub End Class  

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

Open table as spreadsheet

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

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




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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