My question is a simple one:
Is there any way to make an array structure a part of a condition?
For example:
orCode:If array = {0,1,2,3} Then
...
Thanks in advance. :)Code:If array1 = array2 Then
...
Printable View
My question is a simple one:
Is there any way to make an array structure a part of a condition?
For example:
orCode:If array = {0,1,2,3} Then
...
Thanks in advance. :)Code:If array1 = array2 Then
...
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.
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.
What the hell?
That's a surprise, as one could think that it's a rather basic maneuver.
Thanks for the quick answer.
By the way, shouldn't something like this work somehow?
Code:If array Is {1, 2, 3, 4} Then
...
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.
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.
It's simple enough to write your own extension method and then use them wherever you need such a comparison.vb.net Code:
Imports System.Runtime.CompilerServices Public Module ArrayExtensions <Extension()> Public Function IsEqualTo(Of T)(source As T(), array As T()) As Boolean If source.Length <> array.Length Then Return False End If For index = 0 To source.GetUpperBound(0) If Not source(index).Equals(array(index)) Then Return False End If Next Return True End Function End Modulevb.net Code:
Dim arr1 = {1, 2, 3} Dim arr2 = {1, 2, 3} Dim arr3 = {1, 2, 3, 4} Dim arr4 = {3, 2, 1} Dim arr5 = {4, 5, 6} MessageBox.Show((arr1.IsEqualTo(arr2)).ToString()) MessageBox.Show((arr1.IsEqualTo(arr3)).ToString()) MessageBox.Show((arr1.IsEqualTo(arr4)).ToString()) MessageBox.Show((arr1.IsEqualTo(arr5)).ToString())
Thank you all for your answers,
jmcilhinney you code helped me a lot, thank you.