Skip to content

Latest commit

 

History

History
75 lines (62 loc) · 3.58 KB

File metadata and controls

75 lines (62 loc) · 3.58 KB
title Of Clause (Visual Basic)
ms.date 07/20/2015
ms.prod .net
ms.reviewer
ms.suite
ms.technology
devlang-visual-basic
ms.topic article
f1_keywords
Of
vb.Of
vb.of
helpviewer_keywords
Of keyword [Visual Basic]
arguments [Visual Basic], data types
constraints, Visual Basic generic types
generic parameters
generics [Visual Basic], constraints
parameters [Visual Basic], type
types [Visual Basic], generic
parameters [Visual Basic], generic
type parameters
data type arguments
ms.assetid 0db8f65c-65af-4089-ab7f-6fcfecb60444
caps.latest.revision 17
author dotnet-bot
ms.author dotnetcontent

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 xref:System.IComparable interface, which means the consuming code must supply a type argument that implements xref:System.IComparable. This is necessary so that the add procedure can call the xref:System.IComparable.CompareTo%2A?displayProperty=nameWithType 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 xref:System.IComparable.

The following code example creates an object that holds String entries and associates an Integer key with each one. Integer implements xref:System.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

xref:System.IComparable
Type List
Generic Types in Visual Basic
In
Out