使用ReDim preserve在Visual Basic 6多维数组 [英] ReDim Preserve to a Multi-Dimensional Array in Visual Basic 6

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

问题描述

我使用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 ,只有最后一个数组的尺寸( ReDim语句MSDN上):

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

如果您使用preserve关键字,你可以调整只有最后一个数组
  尺寸,你可以不会改变的维数。对于
  例如,如果你的数组只有一个尺寸,你可以调整的
  维,因为它是最后的,唯一的尺寸。但是,如果您
  阵列具有两个或更多的尺寸,可以改变的唯一的大小
  最后一个维度,仍然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

因此​​,决定第一个问题是2维数组是否是这个职位的最佳数据结构。也许,一维数组是一个更适合的,你需要做使用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?

另一种方法是使用交错数组按彼得Geerkens的建议。有在VB6交错数组不直接支持。至code在VB6数组的数组的一种方法是申报变异的数组,使每个元素所需类型的数组(字符串你的情况)。演示code如下。

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 你自己的一部分。对于您需要创建数据的副本是preserved然后填写redimensioned数组吧。

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

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

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