Solved

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

Posted on 2013-12-11
2
264 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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