INSTANTIATING GENERIC CLASSES

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 an Of clause. 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 pass parameters to 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 T)
    Public MyData As T
    Public Children As New List(Of TreeNode(Of T))
   
    Public Sub New(ByVal new_data As T)
        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)
 

Example program GenericTree, which is available for download on the book’s website, uses similar code to build a generic Tree(Of T) class.

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 instead of the more cumbersome 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)
...
 

Example program GenericTreeImportsAlias demonstrates this approach.

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(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)
...
 

Example program GenericTreeSubclass demonstrates this approach.

If you use this technique, you can also add extra convenience functions to the derived class. For example, the following code shows a new EmployeeNode constructor that creates the Employee object that it holds:

Public Sub New(first_name As String, last_name As String)
    MyBase.New(New Employee(first_name, last_name))
End Sub
 
..................Content has been hidden....................

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