Results 1 to 3 of 3

Thread: Looping Tutorial

  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

    Looping Tutorial

    Hello Everyone:

    Can anyone tell me of a good looping tutorial? I am interested in a double loop; I think that’s what it is called. Something like this

    Loop 1
    Do this 10 times
    Do this
    Loop 2
    Do this 6 times
    Do something else
    End loop 2
    Do something different
    End loop 1

    I hope this makes sense?

    Thanks

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

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

    Re: Looping Tutorial

    What you're talking about is called nesting, i.e. the inner loop is nested inside the outer loop. It's really no big deal. The inner loop works just like any other loop; it just happens to be inside a block that just happens to be a loop. Nested loops are most often used to traverse multi-dimensional data structures, e.g. the following code will visit every row of a DataGridView and then visit each cell in the current row:
    vb.net Code:
    1. For Each row As DataGridViewRow In Me.DataGridView1.Rows
    2.     For Each cell As DataGridViewCell In row.Cells
    3.         MessageBox.Show(cell.Value.ToString())
    4.     Next
    5. Next
    That uses For Each loops, which are more natural in situations that suit them. You can also use For loops:
    vb.net Code:
    1. For rowIndex As Integer = 0 To Me.DataGridView1.RowCount - 1
    2.     For columnIndex As Integer = 0 To Me.DataGridView1.ColumnCount - 1
    3.         MessageBox.Show(Me.DataGridView1(columnIndex, rowIndex).Value.ToString())
    4.     Next
    5. Next
    You can put whatever code you like inside either loop, but obviously you can only use variables that are in scope, e.g. you could only access the loop counter for the inner loop, i.e. cell or columnIndex, inside the inner loop.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Looping Tutorial

    I should also point out that the code snippets above will both traverse the data across and then down. This is the most natural way to read data in a DataGridView but not the only way. The For Each loop above results in easier-to-read code but does not adapt well to reading the data down and then across. In the case of the For loops, you simply have to reverse their positions and the data will be read by column then by row rather than by row and then by column:
    vb.net Code:
    1. For columnIndex As Integer = 0 To Me.DataGridView1.ColumnCount - 1
    2.     For rowIndex As Integer = 0 To Me.DataGridView1.RowCount - 1
    3.         MessageBox.Show(Me.DataGridView1(columnIndex, rowIndex).Value.ToString())
    4.     Next
    5. 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