
Nested Generic Types | 139
Generics
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
MsgBox(theText.ToUpper( ))
End Sub
End Class
Even if you create an instance of this class with a String:
Dim usingStringNow As UsuallyString(Of String)
the original class definition will not compile because the compiler does not know
in advance that you plan to use it with
String data.
If you place an interface type constraint on a type parameter, uses of that type
parameter can access the members of the interface, as shown in the following
code.
Public Interface ISimple
Sub WriteThisCode( )
End Interface
Public Class UsesISimple(Of T As ISimple)
Public Sub DoTheWork(ByVal theData As T)
' ----- Uses the class-specific implementation.
theData.WriteThisCode( )
End Sub
End Class
If you have multiple type constraints on a type parameter, uses of that parameter
can generally access all members of all type constraints. If name conflicts do exist
between two constraints (such as a class and interface having a member of the
same name), your code will have to cast or convert the typed object to the desired
class or interface (using
CType or some other method) before calling the conflicting
member.
Generic Methods
Type members (functions and sub procedures) can also be defined with type
parameters, whether or not the class in which they appear uses generics. The type ...