Link to home
Start Free TrialLog in
Avatar of IvanHowarth
IvanHowarth

asked on

VB.net (v.1) Resizing multidimensional array

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).
Avatar of sirbounty
sirbounty
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
no love... :(