Solved (v.1) Resizing multidimensional array

Posted on 2007-03-21
Last Modified: 2013-11-07
I have a 2D array, to get...

Row      Element1      Element2
0      Hello            World

I use this code...

Dim ar(,) As String = New String(0, 1) {}
ar(0, 0) = "Hello"
ar(0, 1) = "World"

But if I want an unknown number of rows at run time, say 3...

Row      Element1      Element2
0      Hello            World
1      Hello            World
2      Hello            World      

What code do I use? How do I start with an empty array size, then ReDim Preserve for each time I need to add another row? Or is there another way of resizing by 1 whilst preserving the data. I do need two elements (Columns).
Question by:IvanHowarth
  • 2
LVL 67

Expert Comment

ID: 18763798
you'll only be able to redim the rightmost dimension.

As an example...

Dim ar(1,0) As String
ar(0,0) = "Hello"

Dim x As Int16=1
Redim Preserve ar(1,x) 'add a row

Redim Preserve ar(1,x) 'add a row
LVL 25

Accepted Solution

dstanley9 earned 500 total points
ID: 18764630
For an unknown number of rows, use an ArrayList.  If your column size is fixed, you can use:

Dim ar As ArrayList = New ArrayList();
ar(0) = new string(2) {"Hello", "World"}
ar(1) = new string(2) {"Hello", "World"}

Unfortunately ArrayList is not type-safe, so you will have to cast each item to a string array:

String() item = CType(ar(0),String())
string vol2 = item(1)

LVL 67

Expert Comment

ID: 18765085
no love... :(

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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