Is 運算子 (Visual Basic)
比較兩個物件參考變數。
result = object1 Is object2
組件
result
必要項。 任何 Boolean 值。object1
必要項。 任何 Object 名稱。object2
必要項。 任何 Object 名稱。
備註
Is 運算子會判斷兩個物件參考是否代表同一物件。 但是,它不會執行值比較。 如果 object1 和 object2 都會參考完全相同的物件執行個體,則 result 為 True,如果不是,則 result 為 False。
Is 也可與 TypeOf 關鍵字搭配使用,以組成 TypeOf...Is 運算式,用以測試物件變數是否會與資料型別相容。
注意事項 |
---|
Is 關鍵字可以用於 Select...Case 陳述式 (Visual Basic)。 |
範例
下列範例會使用 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 運算子來測試早期繫結 (Early Bound) 和晚期繫結 (Late Bound) 物件。