Rediger

Del via


Of Clause (Visual Basic)

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.

Using the Of Keyword

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

See also