using multiple conditions in the same for loop

Lums

Member
Joined
Feb 9, 2010
Messages
22
Programming Experience
Beginner
i want to set 2 conditions for a for loop as i want to loop through the same line of code at the same time . here it is as i have it now


it does not work how i want it to as i want both loops to run simuntaneously instead it runs the first loops continuously before the second
and way around this?
For Me.i = 0 To maxrows - 1
For Me.h = 1 To maxrows
Me.Controls.Item("TextBox" & Me.h).Text = ds.Tables("TestCriteria").Rows(Me.i).Item(0)


CType(Me.Controls.Item("NumericUpDown" & Me.h), NumericUpDown).Value = Convert.ToDecimal(ds.Tables("TestCriteria").Rows(Me.i).Item(1))
Next
Next
 
That's not how For loops work. A For loop has a single loop counter, and a single loop counter is all you need:
VB.NET:
For index As Integer = 0 To maxRows - 1
    Dim count As Integer = index + 1
    Dim row As DataRow = ds.Tables("TestCriteria").Rows(index)

    Me.Controls("TextBox" & count).Text = CStr(row(0))
    DirectCast(Me.Controls("NumericUpDown" & count), NumericUpDown).Value = CDec(row(1))
Next
Better still, you can put your controls into an array or collection. That way you don't even need the offset because the index of the row will match the index of the controls:
VB.NET:
Private textBoxes As New List(Of TextBox)
Private numericUpDowns As New List(Of NumericUpDown)
VB.NET:
For index As Integer = 0 To maxRows - 1
    Dim row As DataRow = ds.Tables("TestCriteria").Rows(index)

    Me.textBoxes(index).Text = CStr(row(0))
    Me.numericUpDowns(index).Value = CDec(row(1))
Next
 
Back
Top