option strict on

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 Boolea

' 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 compariso
'Return A Is

End Functio

If option strict is off and A = "AAA" and B = "AAA"

Return A = B ' Value type standard compariso
returns Tru

with option strict o
Return A Is
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......
 
droope said:
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.
 
droope,
In addition to Peter's comments, have you tried using Object.Equals instead?

Return A.Equals(B)
Return A = B ' Value type standard comparison
'Return A Is B
Why doesn't Return A Is B return true if the values are the same?
As Peter stated, when you pass values as Object you are receiving references
to objects on the heap, although they may have the "same value" they have
different "references" as they are distinct objects. The Is operator always
compares references!

Hope this helps
Jay
 
Hi Droope,

In addition to the others but written in another way

A IS B the have different names but they are the same (reference to the
same adres)

A = B the are different but have the same value (and are from the same type
to do this check)

Cor
 
Back
Top