By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 717 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
4 Replies


waynespangler
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

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

waynespangler
P: 8
Try this then:

Expand|Select|Wrap|Line Numbers
  1.  Public Class Form1
  2.     Dim ary1() As Integer = {1, 2, 3, 4}
  3.     Dim ary2() As Integer = {7, 8, 9}
  4.     Dim ary3(,) As Integer
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         Dim x As Integer
  8.         Dim arySize As Integer = UBound(ary1)
  9.         If UBound(ary2) > arySize Then arySize = UBound(ary2)
  10.         ReDim ary3(arySize, 1)
  11.         For x = 0 To UBound(ary1)
  12.             ary3(x, 0) = ary1(x)
  13.         Next
  14.         For x = 0 To UBound(ary2)
  15.             ary3(x, 1) = ary2(x)
  16.         Next
  17.     End Sub
  18. End Class 
Sep 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.