Dynamic 2D Arrays

Started by
3 comments, last by vbisme 23 years, 2 months ago
How do you do dynamic 2D arrays in Visual Basic? In 1D you would do: Dim myArray() As Long this: Dim myArray()() As Long for two dimension won''t work. How do you do it?
Advertisement
I know next to zero Visual Basic but try this:

If there is a typing mechanism in which you can define your own types, then make a single dimensional array of longs a type.

Then do this:

Dim myArray () as MyArrayType

It might work but I couldn''t tell ya.

If you know C/C++ then this is idea I''m getting at:

typedef long[20] array1d;
typedef array1d[20] array2d;

So that a 2D array is basically an array of arrays.
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Robot, the problem with that is that it wouldn''t be "dynamic."

In C++ I would probably use pointers. Just that Visual Basic has no pointers so I don''t know what to do.
Dim MyArray() As IntegerRedim MyArray(10, 10) 


It is that simple. But remember that because VB and C++ are so closely linked, that if you need to use ReDim preserve, there will be a restriction on one of the dimensions (can''t remember which - you probably can''t change the first one).

Alternatively, you could create a dynamic array of a type which has a dynamic array in it, like this:

Type ArrayThing Something() As IntegerEnd TypeDim SuperDynamicArray() As ArrayThing 


With this type of array, you can have really weird things happening, like 10 rows on the first column, then 5 rows on the next, then 50 on the next. So when you resize it, if you want a rectangle, then you must resize all the rows separately.

Trying is the first step towards failure.
Trying is the first step towards failure.
I will assume you know how to declare and work with single dimension dynamic arrays, here is how to add & remove dimensions from a two dimensional dynamic array. Since the PRESERVE keyword only works on the last dimension of a dynamic array, your going to have to create a temporary array to save your inital array. Then use REDIM without the PRESERVE to modify your array size. Then copy your save array back into your main array as such:


'Define your array type without a dimension
Dim MyArray() As Integer
Dim Saved() As Integer

'Dimension your array's initial size
'Were Rows & Collumns equal the size of the array
Redim MyArray(Rows, Collumns)

'***********************************************

'Call this before redimensioning your main array
Public Sub SaveArray()

Dim Row as Integer
Dim Collumn as Integer
Redim Saved( UBound(MyArray,1) , UBound(MyArray,2) )

For Row = LBound(MyArray,1) to UBound(MyArray,1)
For Collumn = LBound(MyArray,2) to UBound(MyArray,2)
Saved(Row,Collumn) = MyArray(Row,Collumn)
Next Collumn
Next Row

End Sub

'***********************************************

'Add a new row to end of an array
'Will preserve previous dimensions
Redim MyArray( (UBound(MyArray,1)+1) , (UBound(MyArray,2)) )

'Remove the last row from an array
'Will preserve previous dimensions
Redim MyArray( (UBound(MyArray,1)-1) , (UBound(MyArray,2)) )

'***********************************************

'Add a new collumn to end of an array
Redim MyArray( (UBound(MyArray,1)) , (UBound(MyArray,2)+1) )

'Remove the last collumn from an array
Redim MyArray( (UBound(MyArray,1)) , (UBound(MyArray,2)-1) )

'***********************************************

'Add an entire dimension to an array
Redim MyArray( (UBound(MyArray,1)+1) , (UBound(MyArray,2)+1) )

'Remove an entire dimension of an array
Redim MyArray( (UBound(MyArray,1)-1) , (UBound(MyArray,2)-1) )

'***********************************************

'Call this after redimensioning your main array
Public Sub RestoreArray()

Dim Row as Integer
Dim Collumn as Integer

'Copy Contents of Smaller Saved to Larger MyArray
If (UBound(Saved,1) =< UBound(MyArray,1)) AND _
(UBound(Saved,2) =< UBound(MyArray,2)) THEN
For Row = LBound(Saved,1) to UBound(Saved,1)
For Collumn = LBound(Saved,2) to UBound(Saved,2)
MyArray(Row,Collumn) = Saved(Row,Collumn)
Next Collumn
Next Row
Redim Saved(0,0) 'Might as well free up this memory
Exit Sub
End IF

'Copy Contents of Larger Saved to Smaller MyArray
'Some values will be lost pending size differences
If (UBound(Saved,1) >= UBound(MyArray,1)) AND _
(UBound(Saved,2) >= UBound(MyArray,2)) THEN
For Row = LBound(MyArray,1) to UBound(MyArray,1)
For Collumn = LBound(MyArray,2) to UBound(MyArray,2)
MyArray(Row,Collumn) = Saved(Row,Collumn)
Next Collumn
Next Row
Redim Saved(0,0) 'Might as well free up this memory
End IF

End Sub
'***********************************************

Of course you should add code to handle the case in which you attempt to remove a dimension that is the lower bound of the array structure, otherwise it's possible to get a runtime error.

Keep in mind I haven't fully tested this code, but it should more or less work. You might have to tweak it a bit, but this is the general idea for achieving fully dimensionable dymanic arrays in Visual Basic.

Check the VB help files for more information.

Edited by - Xorcist on February 1, 2001 3:52:28 AM

This topic is closed to new replies.

Advertisement