ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6 ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6 arrays arrays

ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6


As you correctly point out, one can ReDim Preserve only the last dimension of an array (ReDim Statement on MSDN):

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. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array

Hence, the first issue to decide is whether 2-dimensional array is the best data structure for the job. Maybe, 1-dimensional array is a better fit as you need to do ReDim Preserve?

Another way is to use jagged array as per Pieter Geerkens's suggestion. There is no direct support for jagged arrays in VB6. One way to code "array of arrays" in VB6 is to declare an array of Variant and make each element an array of desired type (String in your case). Demo code is below.

Yet another option is to implement Preserve part on your own. For that you'll need to create a copy of data to be preserved and then fill redimensioned array with it.

Option ExplicitPublic Sub TestMatrixResize()    Const MAX_D1 As Long = 2    Const MAX_D2 As Long = 3    Dim arr() As Variant    InitMatrix arr, MAX_D1, MAX_D2    PrintMatrix "Original array:", arr    ResizeMatrix arr, MAX_D1 + 1, MAX_D2 + 1    PrintMatrix "Resized array:", arrEnd SubPrivate Sub InitMatrix(a() As Variant, n As Long, m As Long)    Dim i As Long, j As Long    Dim StringArray() As String    ReDim a(n)    For i = 0 To n        ReDim StringArray(m)        For j = 0 To m            StringArray(j) = i * (m + 1) + j        Next j        a(i) = StringArray    Next iEnd SubPrivate Sub PrintMatrix(heading As String, a() As Variant)    Dim i As Long, j As Long    Dim s As String    Debug.Print heading    For i = 0 To UBound(a)        s = ""        For j = 0 To UBound(a(i))            s = s & a(i)(j) & "; "        Next j        Debug.Print s    Next iEnd SubPrivate Sub ResizeMatrix(a() As Variant, n As Long, m As Long)    Dim i As Long    Dim StringArray() As String    ReDim Preserve a(n)    For i = 0 To n - 1        StringArray = a(i)        ReDim Preserve StringArray(m)        a(i) = StringArray    Next i    ReDim StringArray(m)    a(n) = StringArrayEnd Sub


Since VB6 is very similar to VBA, I think I might have a solution which does not require this much code to ReDim a 2-dimensional array - using Transpose, if you are working in Excel.

The solution (Excel VBA):

Dim n, m As Integern = 2m = 1Dim arrCity() As VariantReDim arrCity(1 To n, 1 To m)m = m + 1ReDim Preserve arrCity(1 To n, 1 To m)arrCity = Application.Transpose(arrCity)n = n + 1ReDim Preserve arrCity(1 To m, 1 To n)arrCity = Application.Transpose(arrCity)

What is different from OP's question: the lower bound of arrCity array is not 0, but 1. This is in order to let Application.Transpose do it's job.

Note that Transpose is a method of the Excel Application object (which in actuality is a shortcut to Application.WorksheetFunction.Transpose). And in VBA, one must take care when using Transpose as it has two significant limitations: If the array has more than 65536 elements, it will fail. If ANY element's length exceed 256 characters, it will fail. If neither of these is an issue, then Transpose will nicely convert the rank of an array form 1D to 2D or vice-versa.

Unfortunately there is nothing like 'Transpose' build into VB6.


In regards to this:

"in my task I have to change the whole array (2 dimensions"

Just use a "jagged" array (ie an array of arrays of values). Then you can change the dimensions as you wish. You can have a 1-D array of variants, and the variants can contain arrays.

A bit more work perhaps, but a solution.