Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-11
2
Medium Priority
?
269 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 2000 total points
ID: 39711993
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
ID: 39712012
Thanks ChloesDad...I didn't use that exact code but it did jar my memory and now I got it working.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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 …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 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