For-Next with For-Next and ArrayList

hanzie

Member
Joined
Mar 19, 2008
Messages
7
Programming Experience
Beginner
Hello, I have the folowing code:

VB.NET:
Dim n As Integer = 0
        For n = 0 To intarray - 1
            table2.Cell((1 + n), 1).Range.Text = myarraylist(n + 0)
            table2.Cell((1 + n), 2).Range.Text = myarraylist(n + 1)
            table2.Cell((1 + n), 3).Range.Text = myarraylist(n + 2)
        Next

This code fills a table with 3 columns with values from a array-list called "myarraylist".
It fills the first row with the first 3 values in the array-list.

But after the first loop instead of filling the second row with the 3th, 4th en 5th value of the arraylist, it fills the second row again with the 2nd and 3th row and with a new 4th value.

How can i make it add again and again upcoming values from the arraylist, and not repeating earlier values from the arraylist?

I know it is in the line: myarraylist(n + 0)

Do I have to make a for-next in the for-next??

Please help. Appreciated!!
 
The problem is in your myarraylist(n + 0) statement. In your first itteration you're sending in 0 + 0 = 0, 0 + 1 = 1, 0 + 2 = 2.

In your second itteration you're getting indices in 1 + 0 = 1, 1 + 1 = 2, 1 + 2 = 3. It sounds like you want to get 3, 4, & 5.

VB.NET:
        Dim row As Integer = 1
        For n As Integer = 0 To myarraylist.Count - 3 Step 3
            table2.Cell((row), 1).Range.Text = myarraylist(n + 0).ToString()
            table2.Cell((row), 2).Range.Text = myarraylist(n + 1).ToString()
            table2.Cell((row), 3).Range.Text = myarraylist(n + 2).ToString()
            row += 1
        Next

You could do a nested For statement with the rows by calculating (myarraylist.Count - 1)/3 to get the number of rows but I don't see the advantage.
 
Last edited:
Back
Top