A generic class declaration is a declaration that requires type parameters to be supplied in order to form types. Declaring a class in this way makes the class more robust and gives you more power over what goes on within the class. Constraints allow you to control the argument that is passed to the class. If you set a constraint of IComparable then the argument you passed must implement that Interface.
Using generics with collections make it easier to design a single collection for several types. Let’s say that you have a called “vbCity” and another “MicrosoftVB” and you want to use them with a strong-typed collection. For example purposes the collection class name will be “GenericCollection.” Using generic with the GenericCollection class allows you to use that same class for both our vbCity and MicrosoftVB classes. Below is our GenericCollection class with two constraints IComparable and New, the New constraint requires the argument to have a constructor.
Public Class GenericCollection(Of t As {IComparable, New})
Inherits CollectionBase
' Default constructor
Public Sub New()
MyBase.New()
End Sub
' Add a new item to the collection
Public Function Add(ByVal value As t) As Int32
Return MyBase.InnerList.Add(value)
End Function
' Insert item item to the collection
Public Sub Insert(ByVal index As Int32, ByVal value As t)
MyBase.InnerList.Insert(index, value)
End Sub
' Remove an item from the list
Public Sub Remove(ByVal value As t)
MyBase.InnerList.Remove(value)
End Sub
' Sort the collection
Public Sub Sort()
MyBase.InnerList.Sort()
End Sub
' Check if collection contains item
Public Function Contains(ByVal value As t) As Boolean
Return MyBase.InnerList.Contains(value)
End Function
' Setup the default property to Item
Default ReadOnly Property Item(ByVal index As Int32) As t
Get
' Avoid Late-Binding and return this as t type
Return CType(Me.InnerList.Item(index), t)
End Get
End Property
End Class
Notice that everywhere that t is used in the above example will be replaced during runtime with the type of the argument passed to the class. This is how generics make code more robust and reusable. Below is our vbCity class that meets the requirements of the constraints, it implements IComparable and it has a default constructor.
Public Class vbCity
Implements IComparable
Private _userID As Int32
Public Sub New()
MyBase.New()
End Sub
Public Property UserID() As Int32
Get
Return _userID
End Get
Set(ByVal value As Int32)
_userID = value
End Set
End Property
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim vc As vbCity
If obj Is Nothing Then
Return 1
End If
vc = CType(obj, vbCity)
Return vc.CompareTo(vc.UserID)
End Function
End Class
Now that you have your collection class and your vbCity class setup we have to utilize the code that we just wrote. We do so with the following code
Public Sub BuildCollection()
Dim gcc As New GenericCollection(Of vbCity)
Dim vc As vbCity
For i As Integer = 0 To 3
vc = New vbCity
vc.UserID = i
gcc.Add(vc)
Next
End Sub
This is just starching the tip of generics. The designers of .Net have done an excellent job in designing the FCL.