Results 1 to 11 of 11

Thread: [RESOLVED] [2005] String Arrary comparison problem

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Resolved [RESOLVED] [2005] String Arrary comparison problem

    Hey guys,

    I have a problem with the output I am receiving when I attempt to compare two arrays. It seems that output in lisResults just keeps looping even though I have it on a counter. I am going to alter after I post this but I'm not quite sure where I am going wrong here. Anyone spot something that look quite correct?

    The two string arrays I am comparing each have a single character in each index that can be A thru D. If the same index of each array match I would like LisResults to add "Correct" at the same index. This isn't happening though.
    Code:
            Dim ElementGrade As String
            Dim ElementAnswer As String
            Dim i As Integer
    
    
    
            For Each ElementGrade In GradeArray
                For Each ElementAnswer In AnswerArray
                    For i = 0 To 19
                        If GradeArray(CInt(ElementGrade)) = AnswerArray(CInt(ElementAnswer)) Then
                            Display.LisResults.Items.Add("correct")
                        Else
                            Display.LisResults.Items.Add("incorrect")
    
                        End If
                    Next
                Next
            Next
    
            Display.ShowDialog()
    OK figured out why LisResults was severly spamming creating a huge index. I replaced the code above with the following code:

    Code:
            Dim i As Integer
    
    
    
            For i = 0 To 19
                If GradeArray(i) = AnswerArray(i) Then
                    Display.LisResults.Items.Add("correct")
                Else
                    Display.LisResults.Items.Add("incorrect")
                End If
            Next
    My problem now however is that LisResults is constantly adding "correct" to the index because according to the code above the index of GradeArray and AnswerArray are always going to equal each other because they are on the same counter. When I need to do is access and compare the value inside the given index of each array.
    Last edited by Abrium; Apr 20th, 2007 at 05:07 AM. Reason: advancement of the problem
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

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

    Re: [2005] String Arrary comparison problem

    You've got three loops there. If there were 20 elements in each arrays your If statement would be executed 8000 times. Is that really what you want? I think not.

    Are you saying that you have two arrays, each with 20 elements, and you want to compare the elements at corresponding indices in each? If so then you should only have one loop:
    vb Code:
    1. For i As Integer = 0 To GradeArray.GetUpperBound(0) Step 1
    2.     If GradeArray(i) = AnswerArray(i) Then
    3.         'Correct.
    4.     Else
    5.         'Incorrect.
    6.     End If
    7. Next i
    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

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    Yes I have two arrays, and I edited my post because you were right, as I would looking at it I realized that I had three nested loops... not a good thing. I altered the code now its just the value at the indices
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    I have the following code at this point but it doesn't seem to be pulling the value at the indices, it seems to be working off of the index itself.

    Code:
            For i = 0 To 19
                If GradeArray(i) = AnswerArray(i) Then
                    Display.LisResults.Items.Add("correct")
                Else
                    Display.LisResults.Items.Add("incorrect")
                End If
            Next
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

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

    Re: [2005] String Arrary comparison problem

    That code will do exactly what it looks like it would do: compares each element of GradeArray to the element of AnswerArray at the same index.
    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

  6. #6

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    Thats what I thought. I need to find the error then because if I run the code and completely zero out GradeArray the display form still shows a "correct" indicating a right answer when there isn't an answer at all.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  7. #7

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    This is starting to make me mad. Why the #$&^ is this returning the indices instead of the values of the indices??
    Code:
            count = 0
            For count = 0 To GradeArray.Length - 1
                GradeArray(count) = _
                CStr(Display.LisResults.Items.Add(GradeArray(count)))
            Next
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] String Arrary comparison problem

    Bit of a silly question but are you 100% sure you have got the correct values into the arrays? Just as a test what happens if you do:
    vb Code:
    1. MessageBox.Show("Testing Grade 0: " & GradeArray(0).ToString & " Answer Array 0 is: " & AnswerArray(0).ToString)

    This bit of code doesn't really make much sense, not that it would cause too many problems.
    vb Code:
    1. GradeArray(count) = _
    2.             CStr(Display.LisResults.Items.Add(GradeArray(count)))
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    I have the values correct in two arrays. One I declared, another is filled by an if... then statement but I just can not get these two arrays to compare, going on two days and still charging at it. I'll keep yall updated, if you think of something by all means share.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

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

    Re: [2005] String Arrary comparison problem

    Have you placed a break point on this line:
    vb Code:
    1. If GradeArray(i) = AnswerArray(i) Then
    then added GradeArray(i) and AnswerArray(i) to the Watch window, then stepped through the code? That's how you debug: watch your code in action. Only then can you know exactly what it's doing an why.
    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

  11. #11

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: [2005] String Arrary comparison problem

    I have solved the problem, not the way I would of liked to to be honest. Instead of attempting to compare the arrays which have caused me all of this head ache to begin with I just used the information stored @ each indices of the listboxes I used to show output, I ended up using this code and it seems to work fine. I'm going to debug it a few times to search for mathematical exceptions but I'm sure its on the up and up.

    final code(array free )
    Code:
            For counter = 0 To 19
                If Display.LisAnswers.Items(counter) Is _
                Display.LisCorrect.Items(counter) Then
                    Display.LisResults.Items.Add("right")
                Else
                    Display.LisResults.Items.Add("wrong")
                End If
            Next
    I just need more time/experience with arrays and the way they populate and can be evaluated with the use of a counter. You bring up a very good point also, I should of debugged it in the begining and we see where NOT doing that has put me a day behind just punching in and figuring its going to work... lol I could of been done with this already but at least its solved.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

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