Results 1 to 10 of 10

Thread: Comparing Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Comparing Arrays

    My question is a simple one:

    Is there any way to make an array structure a part of a condition?

    For example:
    Code:
    If array = {0,1,2,3} Then
    ...
    or
    Code:
    If array1 = array2 Then
    ...
    Thanks in advance.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Comparing Arrays

    There isn't a way as simple as what you are looking for, to the best of my knowledge. For one thing, the question is not all that straightforward. Would {1,2,3} be the same array or different from {1,3,2}? In many situations, I would say that they are the same, since they have the same elements, but technically, they are different because they are in a different order.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Comparing Arrays

    No, there is no such comparison. You would have to do it manually, i.e. first compare the Length properties and, if they are equal, then use a For loop to compare each pair of corresponding elements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Comparing Arrays

    What the hell?
    That's a surprise, as one could think that it's a rather basic maneuver.

    Thanks for the quick answer.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Comparing Arrays

    Quote Originally Posted by Shaggy Hiker View Post
    There isn't a way as simple as what you are looking for, to the best of my knowledge. For one thing, the question is not all that straightforward. Would {1,2,3} be the same array or different from {1,3,2}? In many situations, I would say that they are the same, since they have the same elements, but technically, they are different because they are in a different order.
    I mean exactly the same.

    as in:

    a[0] = 1
    a[1] = 2
    a[2] = 3

    AND

    b[0] = 1
    b[1] = 2
    b[2] = 3

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Comparing Arrays

    By the way, shouldn't something like this work somehow?

    Code:
    If array Is {1, 2, 3, 4} Then
    ...

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Comparing Arrays

    I agree that that is what you mean, but I have yet to find a situation where I would not want {1,2,3} = {1,3,2}, so my equality is not strict equality. If you want strict equality, the comparison is quite straightforward.

    Keep in mind what you are asking the compiler to do when comparing the arrays. In lower level languages, an array is interchangeable with the address of the first element of the array. So the array object isn't holding a big blob of bytes that contains all the elements, it is holding the address of the first byte of the first element in the array along with the data type for the array. That's enough information to traverse the array, and is certainly easier to pass around, but what is the alternative? The only viable alternative is an amorphous big blob of bytes, since the array can technically be any size depending on the data type. How would a compiler even generate the comparison you are looking for? Would it compare the two big blob of bytes? Since it can't simultaneously compare each byte, it would have to compare byte(0) against byte(0), then byte(1) against byte(1), and so forth for an indeterminate number of times.

    Technically, that could be done. Of course, it would only be truly right for basic types, since the comparison of an array of objects would be decidedly doubtful. After all, the only comparison that could be done automatically would be a comparison as to whether the two arrays held the SAME objects, not objects with the same value. So you end up with an equality that is of very limitted use since it only works correctly for certain types of arrays, and only if you have the right assumptions about what equality is. Fortunately, you can write your own. Unfortunately, you HAVE to write your own because they didn't bother writing an automatic solution that would work in only a narrow set of situations.
    My usual boring signature: Nothing

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Comparing Arrays

    Quote Originally Posted by Acidic33 View Post
    By the way, shouldn't something like this work somehow?

    Code:
    If array Is {1, 2, 3, 4} Then
    ...
    No. In that example, array is a reference type. It doesn't hold the values in the array, nor is it interchangeable with them. Instead, the variable holds the address in memory of the object (or the address of the first byte of the object). If you compare that to a different array, which is what you appear to be doing, then you are comparing the address of one object to the address of the other object. If they are the same array, such that the two array variables hold the same address, then they will be equal, but that in no way compares the elements in the array. Of course, if the two arrays hold the exact same address, then they certainly are equal, because they are just two names for the exact same thing, but that is most likely not what you intended.
    My usual boring signature: Nothing

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Comparing Arrays

    It's simple enough to write your own extension method and then use them wherever you need such a comparison.
    vb.net Code:
    1. Imports System.Runtime.CompilerServices
    2.  
    3. Public Module ArrayExtensions
    4.  
    5.     <Extension()>
    6.     Public Function IsEqualTo(Of T)(source As T(), array As T()) As Boolean
    7.         If source.Length <> array.Length Then
    8.             Return False
    9.         End If
    10.  
    11.         For index = 0 To source.GetUpperBound(0)
    12.             If Not source(index).Equals(array(index)) Then
    13.                 Return False
    14.             End If
    15.         Next
    16.  
    17.         Return True
    18.     End Function
    19.  
    20. End Module
    vb.net Code:
    1. Dim arr1 = {1, 2, 3}
    2. Dim arr2 = {1, 2, 3}
    3. Dim arr3 = {1, 2, 3, 4}
    4. Dim arr4 = {3, 2, 1}
    5. Dim arr5 = {4, 5, 6}
    6.  
    7. MessageBox.Show((arr1.IsEqualTo(arr2)).ToString())
    8. MessageBox.Show((arr1.IsEqualTo(arr3)).ToString())
    9. MessageBox.Show((arr1.IsEqualTo(arr4)).ToString())
    10. MessageBox.Show((arr1.IsEqualTo(arr5)).ToString())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    15

    Re: Comparing Arrays

    Thank you all for your answers,

    jmcilhinney you code helped me a lot, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width