VB Question

Rather than make another thread, thought I'd put it in here.

Ok so I've now made 2 arrays.

For example;
Array One has 3, 2, 1
Array Two has 1, 2, 3

I want to be able to sort Array One in Ascending Order.

Then say "If Array One = Array Two" MessageBox.Show("Well Done").

and if not equal, MessageBox.Show("Have another go").

-

Problem I am coming across is I am coming across is that Array One will have the numbers mixed up. I'm trying to use the Array.Sort on the entire array.

I'm trying to put, for example, SortedNumbers = Array.Sort(array1)

But it gives the error "expression does not produce a value".

Just wondered if anyone knows how I go about doing this?
 
I'm trying to put, for example, SortedNumbers = Array.Sort(array1)

But it gives the error "expression does not produce a value".

That's because Array.Sort() doesn't return anything. Its a static member.

Array.Sort(array1);

is all u need. "array1" will be sorted.
 
That's because Array.Sort() doesn't return anything. Its a static member.

Array.Sort(array1);

is all u need. "array1" will be sorted.

Yup, but how can I then compare the sorted array with the unsorted array?

I can't compare array1 with array2 because the array1 will be unsorted won't it? unless when you do the sort it applies it to the whole array 1?

Is that what you mean?
 
Yup, but how can I then compare the sorted array with the unsorted array?

I can't compare array1 with array2 because the array1 will be unsorted won't it? unless when you do the sort it applies it to the whole array 1?

Is that what you mean?

Array.Sort( array1) takes array1 and sorts it. array1 will be sorted into whatever order is default.

eg:

array1 = 2, 3, 1
array2 = 1, 2, 3

Array.Sort(array1)

array1 = 1, 2, 3
array2 = 1, 2, 3
 
Last edited:
Ok, and its appearing to compare two arrays I must use the
If array1 Is array2 Then MessageBox.Show("100%") Else Messagebox.Show("Have another go").

Only problem is if I arrange the numbers into ascending order (array2) and compared with the ascending order (array1) I keep getting the "have another go" rather than "100%".
 
Ok, and its appearing to compare two arrays I must use the
If array1 Is array2 Then MessageBox.Show("100%") Else Messagebox.Show("Have another go").

Only problem is if I arrange the numbers into ascending order (array2) and compared with the ascending order (array1) I keep getting the "have another go" rather than "100%".

That's because you can't compare 2 arrays with a simple if statement.

Really, you need to make a new method and pass both arrays to be compared. Then you need to loop thru each of the arrays elements and do a comparison with each element.
 
Try this.

Code:
        Dim Match As Boolean = True
        Dim arr1() As Integer = {1, 2, 3}
        Dim arr2() As Integer = {1, 3, 2}
        Array.Sort(arr1)
        Array.Sort(arr2)
        For i = 0 To arr1.Length - 1
            If arr1(i) = arr2(i) Then
                Match = True
            Else
                Match = False
                Exit For
            End If
        Next
        MsgBox(Match)
 
Back
Top Bottom