如果您有兩個參考物件的變數,您可以使用 Is 或 IsNot 運算符或兩者來判斷它們是否參考相同的實例。
測試兩個物件是否相同
使用 Is運算子 或 IsNot運算子 搭配兩個變數作為運算元。
Public Sub processControl(ByVal f As System.Windows.Forms.Form, ByVal c As System.Windows.Forms.Control) Dim active As System.Windows.Forms.Control = f.ActiveControl If (active IsNot Nothing) And (c Is active) Then ' Insert code to process control c End If Return End Sub
視兩個物件是否參考相同的實例而定,您可能會想要採取特定動作。 先前的範例會比較控件 c 與窗體 f 上的目前活動控件。 如果沒有活動的控制項,或者有一個控制項實例,但不是 c 的同一個實例,則 If 語句會失敗,程式會在未進一步處理的情況下返回。
無論您使用 Is 或 IsNot 是個人便利性的問題。 一個可能比指定表示式中的另一個更容易讀取。