option strict on

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • droope

    #1

    option strict on

    I have a routine that does a standard comparison that I pass two objects to

    Private Function ColumnEqual(ByV al 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......

  • Peter van der Goes

    #2
    Re: option strict on


    "droope" <anonymous@disc ussions.microso ft.com> wrote in message
    news:3410ADCB-9873-483A-AA69-49AD83F80547@mi crosoft.com...[color=blue]
    > I have a routine that does a standard comparison that I pass two objects[/color]
    to.[color=blue]
    >
    > Private Function ColumnEqual(ByV al A As Object, ByVal B As Object) As[/color]
    Boolean[color=blue]
    > '
    > ' Compares two values to determine if they are equal. Also[/color]
    compares DBNULL.Value.[color=blue]
    > '
    > If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both[/color]
    are DBNull.Value.[color=blue]
    > If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only[/color]
    one is DBNull.Value.[color=blue]
    > Return A = B '[/color]
    Value type standard comparison[color=blue]
    > '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.......
    >[/color]
    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]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: option strict on

      droope,
      In addition to Peter's comments, have you tried using Object.Equals instead?

      Return A.Equals(B)
      [color=blue]
      > Return A = B '[/color]
      Value type standard comparison[color=blue]
      > 'Return A Is B[/color]
      [color=blue]
      > Why doesn't Return A Is B return true if the values are the same?[/color]
      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

      "droope" <anonymous@disc ussions.microso ft.com> wrote in message
      news:3410ADCB-9873-483A-AA69-49AD83F80547@mi crosoft.com...[color=blue]
      > I have a routine that does a standard comparison that I pass two objects[/color]
      to.[color=blue]
      >
      > Private Function ColumnEqual(ByV al A As Object, ByVal B As Object) As[/color]
      Boolean[color=blue]
      > '
      > ' Compares two values to determine if they are equal. Also[/color]
      compares DBNULL.Value.[color=blue]
      > '
      > If A Is DBNull.Value And B Is DBNull.Value Then Return True ' Both[/color]
      are DBNull.Value.[color=blue]
      > If A Is DBNull.Value Or B Is DBNull.Value Then Return False ' Only[/color]
      one is DBNull.Value.[color=blue]
      > Return A = B '[/color]
      Value type standard comparison[color=blue]
      > '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.......
      >[/color]


      Comment

      • Cor

        #4
        Re: option strict on

        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


        Comment

        Working...