Of Clause (Visual Basic)

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Introduces an Of clause, which identifies a type parameter on a generic class, structure, interface, delegate, or procedure. For information on generic types, see Generic Types in Visual Basic.

The following code example uses the Of keyword to define the outline of a class that takes two type parameters. It constrains the keyType parameter by the IComparable interface, which means the consuming code must supply a type argument that implements IComparable. This is necessary so that the add procedure can call the IComparable.CompareTo method. For more information on constraints, see Type List.

Public Class Dictionary(Of entryType, keyType As IComparable)  
    Public Sub add(ByVal e As entryType, ByVal k As keyType)  
        Dim dk As keyType  
        If k.CompareTo(dk) = 0 Then  
        End If  
    End Sub  
    Public Function find(ByVal k As keyType) As entryType  
    End Function  
End Class  

If you complete the preceding class definition, you can construct a variety of dictionary classes from it. The types you supply to entryType and keyType determine what type of entry the class holds and what type of key it associates with each entry. Because of the constraint, you must supply to keyType a type that implements IComparable.

The following code example creates an object that holds String entries and associates an Integer key with each one. Integer implements IComparable and therefore satisfies the constraint on keyType.

Dim d As New dictionary(Of String, Integer)  

The Of keyword can be used in these contexts:

Class Statement

Delegate Statement

Function Statement

Interface Statement

Structure Statement

Sub Statement

IComparable
Type List
Generic Types in Visual Basic
In
Out

Show: