Results 1 to 3 of 3

Thread: Help With Nested Loop

  1. #1

    Thread Starter
    Hyperactive Member Art W.'s Avatar
    Join Date
    Apr 2002
    Location
    In My Own Little World, But that’s OK because they know me there!
    Posts
    271

    Help With Nested Loop

    I am trying to run a nested loop; it should call a result from a MDB database. Which it does, when I run the single loop it goes thru all of the entries. But when I try to get a number to display twice it stops on the second entry. Having it display the number twice is just a test to make sure it does work, I am going to modify it after I can prove the loop works. The count on the Message Box is correct, but the number displayed is wrong?

    I want it to display each number twice and then move on to the next number.


    Please see the code below.




    Nested Loop Code:
    1. Do Until number1 = 100
    2.             If number1 <= 0 Then Exit Do
    3.             number1 -= 1
    4.             counter1 += 1
    5.  
    6.             If intCurrentIndex < ds.Tables(0).Rows.Count - 1 Then
    7.                 intCurrentIndex = intCurrentIndex + 1
    8.  
    9.                 Do Until number2 = 10
    10.                     If number2 <= 0 Then Exit Do
    11.                     number2 -= 1
    12.                     counter2 += 1
    13.  
    14.                     txtResults.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Results").ToString()
    15.                 Loop
    16.  
    17.             End If
    18.  
    19.         Loop
    20.  
    21.         MsgBox("The loop ran " & counter1 * counter2 & " times.")








    Any Ideas

    Thanks

    Art W.
    SLEEP: A Totally Inadequate Substation For Caffeine!

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help With Nested Loop

    I suppose number1 > 100 at the beginning, right?

    because it will never become 100 within your cycle and your initial condition (until number1 = 100) won't happen. If it's zero in the beginning then this line will have your parent loop cycle only once:

    Code:
    If number1 <= 0 Then Exit Do

    There's no need to use counter2 you can increment counter1 in the nested loop with the same result.

    For the nested loop I suggest you use For ... Next construction (or For Each if you need to iterate through all of the rows):

    Code:
    For intCurrentIndex = whateverthestartingvalueis To ds.Tables(0).Rows.Count - 1
        counter1 +=1
        txtResults.Text = ds.Tables(0).Rows(intCurrentIndex).Item("Results").ToString()
    Next
    Mind the fact that txtResults.Text will contain only the last item because you assign a new text with each loop. If you need to see all results you should add text to it, not change it.

    Code:
    txtResults.AppendText(ds.Tables(0).Rows(intCurrentIndex).Item("Results").ToString() & Environment.NewLine())

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

    Re: Help With Nested Loop

    Using Do loops to count is a poor choice. That's what For loops are for. If you want to traverse a DataTable then you should use For and/or For Each loops:
    vb.net Code:
    1. For Each row As DataRow In myDataTable.Rows
    2.     For Each column As DataColumn In myDataTable.Columns
    3.         MessageBox.Show(row(column).ToString())
    4.     Next
    5. Next
    vb.net Code:
    1. For rowIndex As Integer = 0 To myDataTable.Rows.Count - 1
    2.     Dim row As DataRow = myDataTable.Rows(rowIndex)
    3.  
    4.     For columnIndex As Integer In myDataTable.Columns.Count - 1
    5.         MessageBox.Show(row(columnIndex).ToString())
    6.     Next
    7. Next
    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

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