Solved

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

Posted on 2013-12-11
2
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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