424,946 Members | 717 Online Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

Trouble with Multidimensional Array Looping.

 P: 28 I'm having problems with my code concerning a mulitdimensional array. Basically, I'm trying to create an array (arrTotal) which contains all of the data from 2 arrays (arrOne & arrTwo). Basically, they each contain data from Tables, and I just want to combine them into a single array which contains all of the info. Here's what I have so far, but I know something isn't right: Dim i As Integer Dim j As Integer 'This gives me the total upper boundaries of both arrays ReDim arrTotal((UBound(arrOne, 1)) + (UBound(arrTwo, 1))) 'intCount2ndDimension contains the Total Number of 2nd Dimensions for 'both arrays For j = 0 To intCount2ndDimension 'Here is where my problem is: I'm trying to fill in the data from both 'arrays into arrTotal, but I'm having problems with the multidimension 'Do I need to add both the 1st and 2nd dimensions separately or can I do it like below?: For i = 0 To UBound(arrOne, 1) arrTotal(i) = arrOne(i, j) Next 'I don't think this nesting is right. I want to have arrTotal contain 'both arrOne and arrTwo: For i = 0 To UBound(arrTwo, 1) arrTotal(i) = arrTwo(i, j) Next Next End Sub Sorry if I wasn't clear enough. I'd appreciate any thoughts. Thanks! Sep 7 '07 #1
4 Replies

 P: 8 Not sure what you want. Ary1 1 2 3 Ary2 7 8 9 Now: Ary3(5)? 1 2 3 7 8 9 -or- Ary3(2,1)? 1 - 7 2 - 8 3 - 9 Sep 7 '07 #2

 P: 28 Thanks for your reply. I think I want it to display like this: Ary3(2,1) 1 - 7 2 - 8 3 - 9 Sep 7 '07 #3

 100+ P: 103 Why would you need two loops Just try: Expand|Select|Wrap|Line Numbers For i = 0 to UBound(arrTwo, 1)   arrTotal(i,0) = arrOne(i)   arrTotal(i,1) = arrTwo(i) Next   Sep 7 '07 #4

 P: 8 Try this then: Expand|Select|Wrap|Line Numbers  Public Class Form1     Dim ary1() As Integer = {1, 2, 3, 4}     Dim ary2() As Integer = {7, 8, 9}     Dim ary3(,) As Integer       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         Dim x As Integer         Dim arySize As Integer = UBound(ary1)         If UBound(ary2) > arySize Then arySize = UBound(ary2)         ReDim ary3(arySize, 1)         For x = 0 To UBound(ary1)             ary3(x, 0) = ary1(x)         Next         For x = 0 To UBound(ary2)             ary3(x, 1) = ary2(x)         Next     End Sub End Class  Sep 7 '07 #5 