Why return false?

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

    #1

    Why return false?

    In VB2005.

    Dim a() As Object = {10, 4}
    Dim b() As Byte = {10, 4}
    MessageBox.Show (a.Equals(b))

    Why return False, but not Ture?
    Thank you


  • Patrice

    #2
    Re: Why return false?

    It compares the object not the array content. As this is two different
    object, it returns false...

    --
    Patrice

    "yxq" <gayxq@163.ne ta écrit dans le message de news:
    Omb17kp7GHA.238 4@TK2MSFTNGP04. phx.gbl...
    In VB2005.
    >
    Dim a() As Object = {10, 4}
    Dim b() As Byte = {10, 4}
    MessageBox.Show (a.Equals(b))
    >
    Why return False, but not Ture?
    Thank you
    >

    Comment

    • yxq

      #3
      Re: Why return false?

      How to compare the object and array like below?

      "Patrice" <scribe@chez.co mдÈëÏûÏ¢ÐÂÎÅ:u 24I5Aq7GHA.2120 @TK2MSFTNGP03.p hx.gbl...
      It compares the object not the array content. As this is two different
      object, it returns false...
      >
      --
      Patrice
      >
      "yxq" <gayxq@163.ne ta écrit dans le message de news:
      Omb17kp7GHA.238 4@TK2MSFTNGP04. phx.gbl...
      >In VB2005.
      >>
      >Dim a() As Object = {10, 4}
      >Dim b() As Byte = {10, 4}
      >MessageBox.Sho w(a.Equals(b))
      >>
      >Why return False, but not Ture?
      >Thank you
      >>
      >
      >
      >

      Comment

      • rowe_newsgroups

        #4
        Re: Why return false?

        I don't know of an automated way, so you'll probably have to write your
        own method (someone please correct me if I'm wrong). First check the
        number of dimensions, as a one dimensional array can't equal a three
        dimensional array, and then I would compare the lengths of both arrays.
        Then you probably have to check each member of the array. By the way,
        why do you need to check equality of arrays?

        Thanks,

        Seth Rowe


        yxq wrote:
        How to compare the object and array like below?
        >
        "Patrice" <scribe@chez.co mдÈëÏûÏ¢ÐÂÎÅ:u 24I5Aq7GHA.2120 @TK2MSFTNGP03.p hx.gbl...
        It compares the object not the array content. As this is two different
        object, it returns false...

        --
        Patrice

        "yxq" <gayxq@163.ne ta écrit dans le message de news:
        Omb17kp7GHA.238 4@TK2MSFTNGP04. phx.gbl...
        In VB2005.
        >
        Dim a() As Object = {10, 4}
        Dim b() As Byte = {10, 4}
        MessageBox.Show (a.Equals(b))
        >
        Why return False, but not Ture?
        Thank you
        >

        Comment

        • Jay B. Harlow

          #5
          Re: Why return false?

          yxq,
          In addition to the other comments.

          2 problems:

          First you are comparing a byte array to an object array, due to the fact you
          have 2 distinct kinds of arrays they won't compare.

          Second Object.Equals(O bject) is a virtual (Overridable) method, allowing
          types that have "identity" to override the method & offer an implementation.
          Such types include Integer, String, DateTime...

          Array does not override this method, as arrays don't have "identity" (in
          select cases, such as yours, they may; but overall they don't). Instead
          relying on the Object.Equals(O bject) implementation.

          Object.Equals(O bject) itself does a Object.Referenc eEquals which compares
          the two object references, not the content of said objects.

          As Rowe suggests you will need to write your own function. I would consider
          making such a function a Generic Function... However with having 2 distinct
          types of arrays this makes a generic function problematic.

          Here is a simply Generic ArrayEquals for VS 2005 (.NET 2.0):

          Public Function ArrayEquals(Of T)(ByVal a() As T, ByVal b() As T) As
          Boolean
          If a.GetLowerBound (0) <b.GetLowerBoun d(0) Then Return False
          If a.GetUpperBound (0) <b.GetUpperBoun d(0) Then Return False
          Dim comparer As EqualityCompare r(Of T) = EqualityCompare r(Of
          T).Default
          For index As Integer = a.GetLowerBound (0) To a.GetUpperBound (0)
          If Not comparer.Equals (a(index), b(index)) Then Return False
          Next
          Return True
          End Function

          It requires both parameters to be 1 dimension arrays.

          --
          Hope this helps
          Jay B. Harlow
          ..NET Application Architect, Enthusiast, & Evangelist
          T.S. Bradley - http://www.tsbradley.net


          "yxq" <gayxq@163.netw rote in message
          news:Omb17kp7GH A.2384@TK2MSFTN GP04.phx.gbl...
          In VB2005.
          >
          Dim a() As Object = {10, 4}
          Dim b() As Byte = {10, 4}
          MessageBox.Show (a.Equals(b))
          >
          Why return False, but not Ture?
          Thank you
          >

          Comment

          Working...