Generics
Create a generic class
Section titled “Create a generic class”A generic type is created to adapt so that the same functionallity can be accessible for different data types.
Public Class SomeClass(Of T) Public Sub doSomething(newItem As T) Dim tempItem As T ' Insert code that processes an item of data type t. End SubEnd ClassInstance of a Generic Class
Section titled “Instance of a Generic Class”By creating an instance of the same class with a different type given, the interface of the class changes depending on the given type.
Dim theStringClass As New SomeClass(Of String)Dim theIntegerClass As New SomeClass(Of Integer)Define a ‘generic’ class
Section titled “Define a ‘generic’ class”A generic class is a class who adapts to a later-given type so that the same functionality can be offered to different types.
In this basic example a generic class is created. It has a sub who uses the generic type T. While programming this class, we don’t know the type of T. In this case T has all the characteristics of Object.
Public Class SomeClass(Of T) Public Sub doSomething(newItem As T) Dim tempItem As T ' Insert code that processes an item of data type t. End SubEnd ClassUse a generic class
Section titled “Use a generic class”In this example there are 2 instances created of the SomeClass Class. Depending on the type given the 2 instances have a different interface:
Dim theStringClass As New SomeClass(Of String)Dim theIntegerClass As New SomeClass(Of Integer)The most famous generic class is List(of )
Limit the possible types given
Section titled “Limit the possible types given”The possible types passed to a new instance of SomeClass must inherit SomeBaseClass. This can also be an interface. The characteristics of SomeBaseClass are accessible within this class definition.
Public Class SomeClass(Of T As SomeBaseClass) Public Sub DoSomething(newItem As T) newItem.DoSomethingElse() ' Insert code that processes an item of data type t. End SubEnd Class
Public Class SomeBaseClass Public Sub DoSomethingElse() End SubEnd ClassCreate a new instance of the given type
Section titled “Create a new instance of the given type”Creating a new intance of a generic type can be done/checed at compile time.
Public Class SomeClass(Of T As {New}) Public Function GetInstance() As T Return New T End FunctionEnd ClassOr with limited types:
Public Class SomeClass(Of T As {New, SomeBaseClass}) Public Function GetInstance() As T Return New T End FunctionEnd Class
Public Class SomeBaseClassEnd ClassThe baseClass (if none given it is Object) must have a parameter less constructor.
This can also be done at runtime through reflection


