Is 運算子 (Visual Basic)

比較兩個物件參考變數。

語法

result = object1 Is object2

組件

result
必要。 任何 Boolean 值。

object1
必要。 任何 Object 名稱。

object2
必要。 任何 Object 名稱。

備註

Is 運算子會判斷兩個物件參考是否參考相同物件。 不過,其不會執行值比較。 如果 object1object2 兩者都參考完全相同的物件執行個體,則 resultTrue;否則,resultFalse

注意

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 運算子來測試早期繫結與晚期繫結物件。

搭配 Is 運算子使用 TypeOf 運算子

Is 運算子也可以搭配 TypeOf 關鍵字使用,以建立 TypeOf...Is 運算式,如此可測試物件變數是否與資料類型相容。 例如:

If TypeOf sender Is Button Then

另請參閱