Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you correctly point out, one can <code>ReDim Preserve</code> only the last dimension of an array (<a href="http://msdn.microsoft.com/en-us/library/aa266231.aspx" rel="nofollow noreferrer">ReDim Statement</a> on MSDN):</p> <blockquote> <p>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</p> </blockquote> <p>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 <code>ReDim Preserve</code>?</p> <p>Another way is to use jagged array as per <a href="https://stackoverflow.com/a/16369934/262403">Pieter Geerkens's suggestion</a>. 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 <code>Variant</code> and make each element an array of desired type (<code>String</code> in your case). Demo code is below.</p> <p>Yet another option is to implement <code>Preserve</code> 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.</p> <pre><code>Option Explicit Public 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:", arr End Sub Private 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 i End Sub Private 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 &amp; a(i)(j) &amp; "; " Next j Debug.Print s Next i End Sub Private 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) = StringArray End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload