electricpete
Technical User
Help tell me that "is" allows us to test whether two object reference variables point to the same object.
I tried to test it with the following code and get some results I don't understand.
The results in the immediate window are:
I can understand why the "=-comparisons" (equals comparisons) are true. It is comparing the default property of range (value) for two ranges that are the same (A3) and coming up true.
I don't understand why the "is-comparisons" (is comparisons) are false. Don't r1 and r2 point to the same object?
Is there some stupid error I made, or some misunderstanding displayed? I'm confused.
I tried to test it with the following code and get some results I don't understand.
Code:
Sub test1()
Sheet1.Select
Names.Add Name:="cellA3", RefersTo:=Range("A3")
Debug.Print "range =-comparison gives " & CStr(Range("cellA3") = Range("a3"))
Debug.Print "range is-comparison gives " & CStr(Range("cellA3") Is Range("a3"))
Dim r1 As Range, r2 As Range
Set r1 = Sheet1.Range("A3")
Set r2 = Sheet1.Range("cellA3")
Debug.Print "object var =-comparison gives " & CStr(r1 = r2)
Debug.Print "object var is-comparison gives " & CStr(r1 Is r2)
End Sub
Code:
range =-comparison gives True
range is-comparison gives False
object var =-comparison gives True
object var is-comparison gives False
I don't understand why the "is-comparisons" (is comparisons) are false. Don't r1 and r2 point to the same object?
Is there some stupid error I made, or some misunderstanding displayed? I'm confused.