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

前端 未结 10 580
孤城傲影
孤城傲影 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

    You can use a user defined type containing an array of strings which will be the inner array. Then you can use an array of this user defined type as your outer array.

    Have a look at the following test project:

    '1 form with:
    '  command button: name=Command1
    '  command button: name=Command2
    Option Explicit
    
    Private Type MyArray
      strInner() As String
    End Type
    
    Private mudtOuter() As MyArray
    
    Private Sub Command1_Click()
      'change the dimensens of the outer array, and fill the extra elements with "1"
      Dim intOuter As Integer
      Dim intInner As Integer
      Dim intOldOuter As Integer
      intOldOuter = UBound(mudtOuter)
      ReDim Preserve mudtOuter(intOldOuter + 2) As MyArray
      For intOuter = intOldOuter + 1 To UBound(mudtOuter)
        ReDim mudtOuter(intOuter).strInner(intOuter) As String
        For intInner = 0 To UBound(mudtOuter(intOuter).strInner)
          mudtOuter(intOuter).strInner(intInner) = "1"
        Next intInner
      Next intOuter
    End Sub
    
    Private Sub Command2_Click()
      'change the dimensions of the middle inner array, and fill the extra elements with "2"
      Dim intOuter As Integer
      Dim intInner As Integer
      Dim intOldInner As Integer
      intOuter = UBound(mudtOuter) / 2
      intOldInner = UBound(mudtOuter(intOuter).strInner)
      ReDim Preserve mudtOuter(intOuter).strInner(intOldInner + 5) As String
      For intInner = intOldInner + 1 To UBound(mudtOuter(intOuter).strInner)
        mudtOuter(intOuter).strInner(intInner) = "2"
      Next intInner
    End Sub
    
    Private Sub Form_Click()
      'clear the form and print the outer,inner arrays
      Dim intOuter As Integer
      Dim intInner As Integer
      Cls
      For intOuter = 0 To UBound(mudtOuter)
        For intInner = 0 To UBound(mudtOuter(intOuter).strInner)
          Print CStr(intOuter) & "," & CStr(intInner) & " = " & mudtOuter(intOuter).strInner(intInner)
        Next intInner
        Print "" 'add an empty line between the outer array elements
      Next intOuter
    End Sub
    
    Private Sub Form_Load()
      'init the arrays
      Dim intOuter As Integer
      Dim intInner As Integer
      ReDim mudtOuter(5) As MyArray
      For intOuter = 0 To UBound(mudtOuter)
        ReDim mudtOuter(intOuter).strInner(intOuter) As String
        For intInner = 0 To UBound(mudtOuter(intOuter).strInner)
          mudtOuter(intOuter).strInner(intInner) = CStr((intOuter + 1) * (intInner + 1))
        Next intInner
      Next intOuter
      WindowState = vbMaximized
    End Sub
    

    Run the project, and click on the form to display the contents of the arrays.

    Click on Command1 to enlarge the outer array, and click on the form again to show the results.

    Click on Command2 to enlarge an inner array, and click on the form again to show the results.

    Be careful though: when you redim the outer array, you also have to redim the inner arrays for all the new elements of the outer array

提交回复
热议问题