Solved

VB.net (v.1) Resizing multidimensional array

Posted on 2007-03-21
3
345 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
[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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

718 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