First off, let's tidy that up a little, so it's easier to see what is going on:
For Each str1 In myarr1
For Each str In myarr
If str = str1 Then
x += 1
Else
y += 1
End If
txtCorrect.Text = x
txtIncorrect.Text = y
Next
Next
That's the same code as yours, just a little cleaner. Now, the count can't be "wrong" because one or the other of "x" and "y" will always be incremented - so the total "x + y" will equal the number of strings in "myarr" multiplied by the number of strings in "myarr1".
So if it doesn't produce the numbers you expect, then there are several possible reasons:
1) x and / or y are not zero before you enter the outer loop
2) The arrays do not contain the data you think they do
3) The result you wanted is not the number of different combinations.
So start by using the debugger: put a breakpoint on the first line, run you app in the debugger, and look at the arrays, and at x and y.
Then follow the code through watching what happens.
"assumed arraylist=[1,2,3]
result arraylist=[2,1,3]
i need to compare like these [1,2],[2,1],[3,3]
result will like this
correct=1
incorrect=2"
Oh, right! That's
easy!
You don't even need code as complicated as you have already.
Dim assumed As Integer() = {1, 2, 3}
Dim result As Integer() = {2, 1, 3}
Dim same As Integer = 0
Dim diff As Integer = 0
If assumed.Length = result.Length Then
For i As Integer = 0 To assumed.Length - 1
If assumed(i) = result(i) Then
same += 1
Else
diff += 1
End If
Next
txtCorrect.Text = same
txtIncorrect.Text = diff
End If
You only need the
If
at all if they might be different sizes.