Using Generics


The previous sections have already shown a few examples of how to use a generic class. The program declares the class and includes whatever data types are required in parentheses. The following code shows how a program might create a generic list of strings:

  Imports System.Collections.Generic ... Dim names As New List(Of String) 

To use a generic class’s constructor, add a second set of parentheses and any parameters after the type specifications. The following statement creates an IntStringList object, passing it the types Integer, String, and Employee. It calls the class’s constructor, passing it the value 100.

  Dim the_employees As New IntStringList(Of Integer, String, Employee)(100) 

If the program needs to use only a few generic classes (for example, a single collection of strings), this isn’t too bad. If the program needs to use many instances of the class, however, the code becomes cluttered.

For example, suppose that the TreeNode class shown in the following code represents a node in a tree. Its MyData field holds some piece of data and its Children list holds references to child nodes.

  Public Class TreeNode(Of DataType)     Public MyData As DataType     Public Children As New List(Of TreeNode(Of DataType))     Public Sub New(ByVal new_data As DataType)         MyData = new_data     End Sub End Class  

The following code uses this class to build a small tree of Employee objects:

  Dim root As New TreeNode(Of Employee)(New Employee("Annabelle", "Ant")) Dim child1 As New TreeNode(Of Employee)(New Employee("Bert", "Bear")) Dim child2 As New TreeNode(Of Employee)(New Employee("Candice", "Cat")) root.Children.Add(child1) root.Children.Add(child2) 

Repeating the nodes’ data types in the first three lines makes the code rather cluttered. Two techniques that you can use to make the code a bit simpler are using an imports alias and deriving a new class. Both of these let you create a simpler name for the awkward class name TreeNode(Of Employee).

Imports Aliases

Normally, you use an Imports statement to make it easier to refer to namespaces and the symbols they contain. However, the Imports statement also lets you define an alias for a namespace entity. To use this to make using generics easier, create an Imports statement that refers to the type of generic class you want to use and give it a simple alias.

For example, the following code is in the DataTreeTest namespace. It uses an Imports statement to refer to a TreeNode of Employee. It gives this entity the alias EmployeeNode. Later, the program can use the name EmployeeNode to create a TreeNode of Employee.

  Imports EmployeeNode = DataTreeTest.TreeNode(Of DataTreeTest.Employee) ... Dim root As New EmployeeNode(New Employee("Annabelle", "Ant")) Dim child1 As New EmployeeNode(New Employee("Bert", "Bear")) Dim child2 As New EmployeeNode(New Employee("Candice", "Cat")) root.Children.Add(child1) root.Children.Add(child2) ... 

Derived Classes

A second method that simplifies using generics is to derive a class from the generic class. The following code derives the EmployeeNode class from TreeNode(Of Employee). Later, it creates instances of this class to build the tree.

  Public Class EmployeeNode     Inherits TreeNode(Of Employee)     Public Sub New(ByVal new_data As Employee)         MyBase.New(new_data)     End Sub End Class ... Dim root As New EmployeeNode(New Employee("Annabelle", "Ant")) Dim child1 As New EmployeeNode(New Employee("Bert", "Bear")) Dim child2 As New EmployeeNode(New Employee("Candice", "Cat")) root.Children.Add(child1) root.Children.Add(child2) ...  




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