Click to See Complete Forum and Search --> : Using a Two-Dimensional Dynamic Array
daneb
October 3rd, 2001, 08:15 PM
Hi.
How will I declare a two-dimensional dynamic array and redimension it later? For instance, I have an array like this:
Array1(x,2)
where there are two columns fixed, and a variable number of rows which can later be redimensioned, to something like this:
x=x+1
Redim Preserve Array1(x,2)
but I got this error:
Run-time error '9':
Subscript out of range
if I declared my array like this:
Dim Array1() as String
If I declared my array like this:
Dim Array1(0,2) as String
I got a Compile error saying that the array was already dimensioned when I reached the Redim line.
Any help is appreciated!
MKSa
October 3rd, 2001, 09:10 PM
You are getting an error because of the keyword Preserve. If you use the Preserve keyword, you can resize only the last array dimension and you can't change the number of dimensions at all. That means you can not change the first dimension, i.e x, but you can change the second dimension, in this case 2.
MKSa
October 3rd, 2001, 09:21 PM
I forgot to add to my previous message the suggestion of switching dimensions so your array becomes Array1(2,x) then if x=x+1 your Redim Preserve Array1(2,x) should work.
daneb
October 3rd, 2001, 10:12 PM
Thanks for the reply.
I have two questions though:
1) I removed the Preserve keyword. Yes, it worked during the array's content initialization to an empty string (""). This worked fine because there were really no values yet. What if the array has valid values already and I really don't want these values to be lost when I redimension it?
2) Does switching dimension also mean switching the row and column references? Does my intention of having Array1(row,col) will now mean Array1(col,row)? My column reference is fixed, while it is the row that varies. And how will I declare my array? Is this correct ---> Dim Array1() as String
MKSa
October 3rd, 2001, 10:31 PM
You start with:
Dim Array1() as String 'Correct start
x=100 'put whatever value you want
Tweek your code so that you fill the two dimensional array with data with the first dimension as 2 and second as x.
Redim Preserve Array1(2,x) 'This will work
x=x+1
Redim Preserve Array1(2,x) 'will work also
So you need to switch from Array1(row,col) to Array1(col,row) and adjust your code to reflect that and that should be trivial.
MKSa
October 3rd, 2001, 10:43 PM
You start with:
Dim Array1() as String 'Correct start
y=100 'put whatever value you want
Redim Array1(2,y) 'here initialize dimensioning your array
'Tweek your code so that you fill the two dimensional array with data with your rows fixed to 2 and columns set to y.
x=70 'put whatever value you want
Redim Preserve Array1(2,x) 'This will work and preserve your data
x=x+1
'if you add more data
Redim Preserve Array1(2,x) 'will work also
So you need to switch from Array1(row,col) to Array1(col,row) and adjust your code to reflect that and that should be trivial.
srinika
October 3rd, 2001, 11:00 PM
Q1:Dim MyArray () as string
' loop to fill the array
' *** Redim - Preserve part ***
'If (no elements added to the array) then
ReDim MyArray(1, 0) as string
'else
ReDim Preserve MyArray(1, UBound(MyArray, 2) + 1) as string
'End If
' code to add the elements to the array
Q2:
only the latter dimension is getting redimensioned so u have to use the Row - Column Accordingly
Srinika
daneb
October 4th, 2001, 02:09 AM
Thanks again. I tried your code suggestion and it worked! I just have a follow-up question. What if not only one but both dimensions of a two-dimensional array vary? For instance, everytime a user inputs dimensions x,y, the array will be redimensioned. How will you use the Redim statement with the Preserve keyword? When I removed the Preserve keyword, there was no error. If I put Preserve, I got the "Subscript out of range" error.
daneb
October 4th, 2001, 02:14 AM
Thank you very much for your code suggestion. I have learned now that only the last dimension can be manipulated.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.