比較兩個對象參考變數。
語法
result = object1 Is object2
組件
result
必須的。 任何 Boolean 值。
object1
必須的。 任何 Object 名稱。
object2
必須的。 任何 Object 名稱。
備註
運算子 Is 會判斷兩個對象參考是否參考相同的物件。 不過,它不會執行值比較。 如果 object1 與 object2 兩者都參考完全相同的物件實例,則為 True;result如果兩者不相同,result則為 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 運算符來測試早期系結和晚期綁定物件。
搭配Is運算子使用 TypeOf 運算子
Is operator 也可以與 關鍵詞搭配 TypeOf 使用,以建立 TypeOf...Is 運算式,以測試物件變數是否與數據類型相容。 例如:
If TypeOf sender Is Button Then