更新:2007 年 11 月
可以定义这样一个类:您将能够依据该类来创建可在不同数据类型上提供相同功能的对象。为此,您可以在定义中指定一个或多个类型参数。 然后,该类将能够充当使用不同数据类型的对象的模板。通过这种方式定义的类称为泛型类。
定义泛型类这种做法的优点在于:您只需定义一次泛型类,代码就可以利用它来创建使用各种数据类型的多个对象。相对于利用 Object 类型定义类而言,这样做的性能将会更好。
除了类之外,您还可以定义和使用泛型结构、接口、过程和委托。
利用类型参数定义类
- 采用常规方式定义类。 
- 紧接在类名之后,添加 (Of typeparameter) 以指定一个类型参数。 
- 如果有一个以上的类型参数,请在括号内列出这些参数(以逗号分隔)。不要重复 Of 关键字。 
- 如果代码是对类型参数执行操作,而不是简单的赋值,请在该类型参数后添加一个 As 子句,以便添加一个或多个约束。约束可保证为该类型参数提供的类型可满足如下的类似要求: - 支持代码执行的运算(如 >) 
- 支持代码访问的成员(如方法) 
- 公开无参数构造函数 
 - 如果未指定任何约束,则代码只能使用 Object 数据类型 支持的那些运算和成员。有关更多信息,请参见 类型列表。 
- 标识要使用提供的类型声明的每个类成员,然后将其声明为 As typeparameter。这适用于内部存储、过程参数和返回值。 
- 确保代码只使用它可提供给 itemType 的任何数据类型所支持的运算和方法。 - 下面的示例定义一个类,用于管理一个非常简单的列表。它将列表保存在内部数组 items 中,并且使用代码可声明列表元素的数据类型。参数化构造函数允许使用代码设置 items 的上限,默认构造函数将此上限设置为 9(总共 10 项)。 - Public Class simpleList(Of itemType) Private items() As itemType Private top As Integer Private nextp As Integer Public Sub New() Me.New(9) End Sub Public Sub New(ByVal t As Integer) MyBase.New() items = New itemType(t) {} top = t nextp = 0 End Sub Public Sub add(ByVal i As itemType) insert(i, nextp) End Sub Public Sub insert(ByVal i As itemType, ByVal p As Integer) If p > nextp OrElse p < 0 Then Throw New System.ArgumentOutOfRangeException("p", _ " less than 0 or beyond next available list position") ElseIf nextp > top Then Throw New System.ArgumentException("No room to insert at ", _ "p") ElseIf p < nextp Then For j As Integer = nextp To p + 1 Step -1 items(j) = items(j - 1) Next j End If items(p) = i nextp += 1 End Sub Public Sub remove(ByVal p As Integer) If p >= nextp OrElse p < 0 Then Throw New System.ArgumentOutOfRangeException("p", _ " less than 0 or beyond last list item") ElseIf nextp = 0 Then Throw New System.ArgumentException("List empty; cannot remove ", _ "p") ElseIf p < nextp - 1 Then For j As Integer = p To nextp - 2 items(j) = items(j + 1) Next j End If nextp -= 1 End Sub Public ReadOnly Property listLength() As Integer Get Return nextp End Get End Property Public ReadOnly Property listItem(ByVal p As Integer) As itemType Get If p >= nextp OrElse p < 0 Then Throw New System.ArgumentOutOfRangeException("p", _ " less than 0 or beyond last list item") End If Return items(p) End Get End Property End Class- 可以依据 simpleList 声明一个类来保存 Integer 值的列表,声明另一个类来保存 String 值的列表,并再声明一个类来保存 Date 值。除了列表成员的数据类型外,依据所有这些类创建的对象的工作方式都相同。 - 使用代码提供给 itemType 的类型变量可以是内部类型(如 Boolean 或 Double)、结构、枚举或任何类型的类(包括应用程序定义的类)。 - 可以使用以下代码测试类 simpleList。 - Public Sub useSimpleList() Dim iList As New simpleList(Of Integer)(2) Dim sList As New simpleList(Of String)(3) Dim dList As New simpleList(Of Date)(2) iList.add(10) iList.add(20) iList.add(30) sList.add("First") sList.add("extra") sList.add("Second") sList.add("Third") sList.remove(1) dList.add(#1/1/2003#) dList.add(#3/3/2003#) dList.insert(#2/2/2003#, 1) Dim s As String = _ "Simple list of 3 Integer items (reported length " _ & CStr(iList.listLength) & "):" _ & vbCrLf & CStr(iList.listItem(0)) _ & vbCrLf & CStr(iList.listItem(1)) _ & vbCrLf & CStr(iList.listItem(2)) _ & vbCrLf _ & "Simple list of 4 - 1 String items (reported length " _ & CStr(sList.listLength) & "):" _ & vbCrLf & CStr(sList.listItem(0)) _ & vbCrLf & CStr(sList.listItem(1)) _ & vbCrLf & CStr(sList.listItem(2)) _ & vbCrLf _ & "Simple list of 2 + 1 Date items (reported length " _ & CStr(dList.listLength) & "):" _ & vbCrLf & CStr(dList.listItem(0)) _ & vbCrLf & CStr(dList.listItem(1)) _ & vbCrLf & CStr(dList.listItem(2)) MsgBox(s) End Sub