แก้ไข

แชร์ผ่าน


IsNot Operator (Visual Basic)

Compares two object reference variables.

Syntax

result = object1 IsNot object2

Parts

  • result

    Required. A Boolean value.

  • object1

    Required. Any Object variable or expression.

  • object2

    Required. Any Object variable or expression.

Remarks

The IsNot operator determines if two object references refer to different objects. However, it doesn't perform value comparisons. If object1 and object2 both refer to the exact same object instance, result is False; if they don't, result is True.

IsNot is the opposite of the Is operator. The advantage of IsNot is that you can avoid awkward syntax with Not and Is, which can be difficult to read.

You can use the Is and IsNot operators to test both early-bound and late-bound objects.

Example

The following code example uses both the Is operator and the IsNot operator to accomplish the same comparison.

Dim o1, o2 As New Object
If Not o1 Is o2 Then MsgBox("o1 and o2 do not refer to the same instance.")
If o1 IsNot o2 Then MsgBox("o1 and o2 do not refer to the same instance.")

Use TypeOf operator with IsNot operator

Starting with Visual Basic 14, you can use the TypeOf operator with the IsNot operator to test whether an object is not compatible with a data type. For example:

If TypeOf sender IsNot Button Then

See also