Solved

VB.net (v.1) Resizing multidimensional array

Posted on 2007-03-21
3
343 Views
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).
0
Comment
Question by:IvanHowarth
  • 2
3 Comments
 
LVL 67

Expert Comment

by:sirbounty
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"
ar(1,0)="World"

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

x=2
Redim Preserve ar(1,x) 'add a row
ar(0,x)="Hello"
ar(1,x)="World
0
 
LVL 25

Accepted Solution

by:
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)

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18765085
no love... :(
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display

730 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