在 Visual Basic 6 中 ReDim 保留为多维数组 [英] ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6

查看:60
本文介绍了在 Visual Basic 6 中 ReDim 保留为多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB6,我需要对多维数组进行 ReDim Preserve:

I'm using VB6 and I need to do a ReDim Preserve to a Multi-Dimensional Array:

 Dim n, m As Integer
    n = 1
    m = 0
    Dim arrCity() As String
    ReDim arrCity(n, m)

    n = n + 1
    m = m + 1
    ReDim Preserve arrCity(n, m)

每当我按照我写的那样做时,我都会收到以下错误:

Whenever I do it as I have written it, I get the following error:

运行时错误 9:下标超出范围

runtime error 9: subscript out of range

因为我只能更改最后一个数组维度,所以在我的任务中我必须更改整个数组(在我的示例中为 2 维)!

Because I can only change the last array dimension, well in my task I have to change the whole array (2 dimensions in my example) !

是否有任何解决方法或其他解决方案?

Is there any workaround or another solution for this?

推荐答案

正如您正确指出的,您可以ReDim Preserve 仅数组的最后一个维度 (MSDN 上的 ReDim 声明):

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

如果使用 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. 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

因此,决定的第一个问题是二维数组是否是作业的最佳数据结构.也许,一维数组更适合您需要执行ReDim Preserve?

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?

另一种方法是根据 Pieter Geerkens 的建议使用锯齿状数组.VB6 中不直接支持锯齿状数组.在 VB6 中编码数组数组"的一种方法是声明一个 Variant 数组并使每个元素成为所需类型的数组(在您的情况下为 String ).演示代码如下.

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.

另一种选择是自己实现 Preserve 部分.为此,您需要创建一个要保留的数据副本,然后用它填充重新维度的数组.

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 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 & a(i)(j) & "; "
        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

这篇关于在 Visual Basic 6 中 ReDim 保留为多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆