Is 运算符 (Visual Basic)

比较两个对象引用变量。

语法

result = object1 Is object2

部件

result
必填。 任何 Boolean 值。

object1
必填。 任何 Object 名称。

object2
必填。 任何 Object 名称。

注解

运算符 Is 确定两个对象引用是否引用同一对象。 但是,它不执行值比较。 如果 object1object2 两者都引用完全相同的对象实例, resultTrueresult ;否则为 False

注释

Is Select... 中也使用该关键字 ...Case 语句

示例:

以下示例使用 Is 运算符比较对象引用对。 结果将分配给一个值,该值表示这两个 Boolean 对象是否相同。

Dim myObject As New Object
Dim otherObject As New Object
Dim yourObject, thisObject, thatObject As Object
Dim myCheck As Boolean
yourObject = myObject
thisObject = myObject
thatObject = otherObject
' The following statement sets myCheck to True.
myCheck = yourObject Is thisObject
' The following statement sets myCheck to False.
myCheck = thatObject Is thisObject
' The following statement sets myCheck to False.
myCheck = myObject Is thatObject
thatObject = myObject
' The following statement sets myCheck to True.
myCheck = thisObject Is thatObject

如前面的示例所示,可以使用 Is 运算符测试早期绑定和后期绑定对象。

将 TypeOf 运算符与 Is 运算符配合使用

Is 运算符还可用于 TypeOf 关键字来生成 TypeOf...Is 表达式,该表达式测试对象变量是否与数据类型兼容。 例如:

If TypeOf sender Is Button Then

另请参阅