Partilhar via


Cláusula Of (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 Tipos genéricos no Visual Basic (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 Lista de tipos (Visual Basic).

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.

O exemplo de código a seguir cria um objeto que contém String entradas e associa um Integer chave com cada um. Integerimplementa IComparable e portanto satisfaz a restrição em 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

Consulte também

Referência

Lista de tipos (Visual Basic)

In (Modificador Genérico) (Visual Basic)

Out (modificador genérico) (Visual Basic)

IComparable

Conceitos

Tipos genéricos no Visual Basic (Visual Basic)