Trouble with Multidimensional Array Looping.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nico3334
    New Member
    • Aug 2007
    • 28

    Trouble with Multidimensional Array Looping.

    I'm having problems with my code concerning a mulitdimensiona l 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((UBoun d(arrOne, 1)) + (UBound(arrTwo, 1)))

    'intCount2ndDim ension contains the Total Number of 2nd Dimensions for
    'both arrays
    For j = 0 To intCount2ndDime nsion

    '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!
  • waynespangler
    New Member
    • Sep 2006
    • 8

    #2
    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

    Comment

    • nico3334
      New Member
      • Aug 2007
      • 28

      #3
      Thanks for your reply. I think I want it to display like this:

      Ary3(2,1)
      1 - 7
      2 - 8
      3 - 9

      Comment

      • Tig201
        New Member
        • Mar 2007
        • 103

        #4
        Why would you need two loops Just try:
        [CODE=vb]
        For i = 0 to UBound(arrTwo, 1)
        arrTotal(i,0) = arrOne(i)
        arrTotal(i,1) = arrTwo(i)
        Next
        [/CODE]

        Comment

        • waynespangler
          New Member
          • Sep 2006
          • 8

          #5
          Try this then:

          Code:
           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

          Comment

          Working...