Solved

Storing a 2-dimensional array within a 1-dimensional array

Posted on 2013-12-11
2
260 Views
Last Modified: 2013-12-11
All I'm trying to do here is store a collection within a collection...the collection being arrays. My problem is the syntax. I have built the 2-dimensional array, now I wish to add it to the 1-dimensional array but not sure how to do it. Here is my code below.

    Dim idx As Integer = 0
    Dim arr1() As String
    Dim arr2(,) As String

    Redim Preserve arr1(idx)

    For x As Integer = 0 To 14
         Redim Preserve arr2(x, y)
         For y As Integer = 0 To 9
              Redim Preserve arr2(x, y)
              arr2(x, y) = "Test " & x & " and " & y        
         Next
    Next

    arr1(idx) = arr2

I'm getting an error where I assign arr2 to arr1. It says "Value of type 2-dimensional array of String cannot be converted to String". I also tried this:

    arr1(idx) = arr2()

The above syntax gave me the error "Number of indices is less than the number of dimensions of the indexed array".
0
Comment
Question by:BlakeMcKenna
2 Comments
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
Comment Utility
Am I correct in that what you want to do is have a 1 dimensional array of pointers to two dimensional arrays? If so then you really want to use a jagged array that has 3 dimensions

Dim Arr1()()() As string =new String()()() {}
or
Dim Arr1(1)(14)(9) As string

idx = 0

     For x As Integer = 0 To 14

         Redim Preserve arr1(idx)(x)( y) ' I have never tried this with jagged arrays so I don't know if it works

         For y As Integer = 0 To 9
          Redim Preserve arr1(idx)(x)( y)
             arr1(idx)((x)(y) = string.format("Test  {0} and {1}", x.tostring , y.tostring)        
         Next
    Next

I have changed the string concatenation to a string.format  as string concatenation is highly inneficient as each & creates a new string.
0
 

Author Closing Comment

by:BlakeMcKenna
Comment Utility
Thanks ChloesDad...I didn't use that exact code but it did jar my memory and now I got it working.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now