Section 10.8. Nested Generic Types


10.8. Nested Generic Types

The type parameters included in a class definition can be used within nested classes:

     Public Class Level1(Of T1, T2)        Public Class Level2           Public theData As T1        End Class     End Class 

However, when you create instances of the outer and inner classes based on different specified parameters, the resulting instances are fully unrelated. For instance, using the Level1 and Level2 classes just defined, the statements:

     Dim first As New Level1(Integer, Double).Level2     Dim second As Level1(Integer, String).Level2     first.theData = 5     second = first    ' This line will not compile 

will not compile successfully. Even though the Level2 classes both contain only an Integer public member, they are not compatible because each class is tied to a different set of data types.

Nested types may also include their own type parameters.

     Public Class Level1(Of T1, T2)        Public Class Level2(Of T3)           Public theData As T3        End Class     End Class 




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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