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

前端 未结 10 389
孤城傲影
孤城傲影 2020-11-29 09:44

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
    ReDi         


        
10条回答
  •  有刺的猬
    2020-11-29 10:00

    This is more compact and respect the intial first position in array and just use the inital bound to add old value.

    Public Sub ReDimPreserve(ByRef arr, ByVal size1 As Long, ByVal size2 As Long)
    Dim arr2 As Variant
    Dim x As Long, y As Long
    
    'Check if it's an array first
    If Not IsArray(arr) Then Exit Sub
    
    'create new array with initial start
    ReDim arr2(LBound(arr, 1) To size1, LBound(arr, 2) To size2)
    
    'loop through first
    For x = LBound(arr, 1) To UBound(arr, 1)
        For y = LBound(arr, 2) To UBound(arr, 2)
            'if its in range, then append to new array the same way
            arr2(x, y) = arr(x, y)
        Next
    Next
    'return byref
    arr = arr2
    End Sub
    

    I call this sub with this line to resize the first dimension

    ReDimPreserve arr2, UBound(arr2, 1) + 1, UBound(arr2, 2)
    

    You can add an other test to verify if the initial size is not upper than new array. In my case it's not necessary

提交回复
热议问题