Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to ReDim two dimension array more than once?

Status
Not open for further replies.

mtb1996

IS-IT--Management
May 23, 2004
76
US
I want to use a two dimension array to store some contents read in from a tab delimited text file. I don't know how many items I will have until I read to the end of the file, but I do know the columns per line is 2. I've worked with resizing single dimension arrays, but never mulit-dimension.

The way I've done this before with one-dimension arrays is to declare a dynamic array, then resize it as needed.

This fails for a two-dimension array. The first ReDim works, but every subsequent ReDim fails with a "Subscript out of range" error. Here's some example code:

Dim strTest() As String

ReDim Preserve strTest(10, 2) As String
ReDim Preserve strTest(20, 2) As String

Can I only ReDim a multi-dimension array once, or am I missing something here?

As always, suggestions for better ways of reading in my text file are appreciated too.

Thanks in advance
 
You are only allowed to redimension the LAST dimension of a multi-dimensional array.

Either combine the two fields into one and split it back apart as needed, or define a class for your data and store references to class instances in your array. Either solution would make it one-dimensional, which means you could redimension it as needed.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
When you use Preserve you can only ReDim the LAST dimension of the array.

Switch it arround so your constant width dimension is first:

Redim strTest(2,10) As String.
 
For an array with 2 or more dimensions you can only resize the last element in the array.

Eg

ReDim strTest(10,2)
ReDim strTest(10,20)

Not the first element in the array.
 
Whoops!

When I started my reply the other two weren't up yet.

Honest!
 
mtb1996,

Further to Tracy's;

I would just turn your array through 90 degrees so that each column stored the data for a record. As in..

Dim strTest() As String

ReDim Preserve strTest(2,10) As String
ReDim Preserve strTest(2,20) As String

regards Hugh
 
I thought of offering the solution of switching the dimension to make it redimensionable, but in this case I think it would confuse things more than help. It's much clearer, and more in keeping with the structure of the data, to keep an array of data "records" and use some other method to store the records.

BTW, this is what M$ refers to proudly as a "Dynamic Array"!


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
For clarity. The following works

ReDim strTest(10, 2)
ReDim strTest(20, 20)
because there is no PRESERVE.

This fails
ReDim strTest(10, 2)
ReDim Preserve strTest(20, 20)



Compare Code
 
Thanks to all,

I eventually found the information, but it was amazing what I had to go through to find it... I did my due diligence before posting searching online KBs, help files, and other threads. Figures that right after I posted I would find the answer in a very old M$ VB Programmer's Guide v5.0 I had. I switched the dimensions so my static was first and all works fine, but I do agree with tsdragon's comment on keeping it an array of data "records". It sucks having to think of my data that way...

So, tsdragon... can you help me with declaring the class for the data?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top