Click here to Skip to main content
16,004,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
assumed = Integer.Parse(txtResult.Text.Trim()) 'Assumed value Manually entered result = Val(txtInput1.Text.Trim()) + Val(txtInput2.Text.Trim())'Actual result

i want to store these two values 'present i am storing these values in arraylist it is giving wrong count when compared'
then i will fetch the correct and incorrect result count after comparesion
VB
For Each str1 In myarr1
                For Each str In myarr
                    If str = str1 Then
                        x += 1
                        txtCorrect.Text = x
                        txtIncorrect.Text = y
                    End If
                    If str <> str1 Then
                        txtCorrect.Text = x
                        y += 1
                        txtIncorrect.Text = y
                    End If
                Next

please help me

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 7-Mar-15 4:11am
v2

1 solution

First off, let's tidy that up a little, so it's easier to see what is going on:
VB
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.
VB
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.
 
Share this answer
 
v2
Comments
devchina 7-Mar-15 11:14am    
how to compare two arraylist index wise in below code?
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
OriginalGriff 7-Mar-15 11:40am    
What exactly are you trying to do - it's not clear from "how to compare two arraylist index wise".
Perhaps an example of your expected input and output would help?
devchina 7-Mar-15 12:02pm    
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
OriginalGriff 7-Mar-15 12:14pm    
Answer updated.
devchina 7-Mar-15 12:23pm    
but i am using arraylist.
arraylist values from textbox:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900