Solved

VB.net (v.1) Resizing multidimensional array

Posted on 2007-03-21
3
325 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
Comment Utility
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
Comment Utility
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
Comment Utility
no love... :(
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now