Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

623 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