"droope" <an*******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
I have a routine that does a standard comparison that I pass two objects
to.
Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As
Boolean '
' Compares two values to determine if they are equal. Also
compares DBNULL.Value. '
If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both
are DBNull.Value. If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only
one is DBNull.Value. Return A = B '
Value type standard comparison 'Return A Is B
End Function
If option strict is off and A = "AAA" and B = "AAA"
Return A = B ' Value type standard comparison
returns True
with option strict on
Return A Is B
returns false.
Why doesn't Return A Is B return true if the values are the same?
I would like to leave option strict on.......
As your A and B are object variables, they are reference types that store
the memory address of the actual object. Your example of assigning the same
string literal to your object variable results in both variables holding the
same address. Thus, with Option Strict off, evaluation of the expression A =
B returns true. As I'm sure you've noticed, if you turn Option Strict on,
use of the = operator with object variables is disallowed.
When you execute your function procedure with Option Strict on, you are
passing two different objects, correct, not two references to the same
object? Using the Is operator to compare two object variables returns true
only if both hold the same address (refer to the same object).
With two object variables assigned to the same string constant, I am unable
to replicate the behavior you cite (with Option Strict on, A Is B returns
false), but if I pass object variables that reference two different objects
to your function procedure, false is always returned.
--
Peter [MVP Academic]