Home All Groups Group Topic Archive Search About
Author
13 Oct 2006 7:29 AM
yxq
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

Author
13 Oct 2006 8:18 AM
Patrice
It compares the object not the array content. As this is two different
object, it returns false...

--
Patrice

"yxq" <ga***@163.net> a écrit dans le message de news:
Omb17kp7GHA.2***@TK2MSFTNGP04.phx.gbl...
Show quoteHide quote
> 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
>
Author
14 Oct 2006 2:52 AM
yxq
How to compare the object and array like below?

Show quoteHide quote
"Patrice" <scr***@chez.com> дÈëÏûÏ¢ÐÂÎÅ:u24I5Aq7GHA.2***@TK2MSFTNGP03.phx.gbl...
> It compares the object not the array content. As this is two different
> object, it returns false...
>
> --
> Patrice
>
> "yxq" <ga***@163.net> a écrit dans le message de news:
> Omb17kp7GHA.2***@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
>>
>
>
>
Author
14 Oct 2006 2:08 PM
rowe_newsgroups
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:
Show quoteHide quote
> How to compare the object and array like below?
>
> "Patrice" <scr***@chez.com> дÈëÏûÏ¢ÐÂÎÅ:u24I5Aq7GHA.2***@TK2MSFTNGP03.phx.gbl...
> > It compares the object not the array content. As this is two different
> > object, it returns false...
> >
> > --
> > Patrice
> >
> > "yxq" <ga***@163.net> a écrit dans le message de news:
> > Omb17kp7GHA.2***@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
> >>
> >
> >
> >
Author
14 Oct 2006 3:15 PM
Jay B. Harlow
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(Object) 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(Object) implementation.

Object.Equals(Object) itself does a Object.ReferenceEquals 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.GetLowerBound(0) Then Return False
        If a.GetUpperBound(0) <> b.GetUpperBound(0) Then Return False
        Dim comparer As EqualityComparer(Of T) = EqualityComparer(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


Show quoteHide quote
"yxq" <ga***@163.net> wrote in message
news:Omb17kp7GHA.2384@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
>