1

很抱歉这个菜鸟问题,但目前如果我需要多数组(数组数组),我将其声明为变体:

Dim ma() As Variant
ReDim ma(1 To 3)
ma(1) = Array(1, 2, 3)

除了它有效之外,我想知道是否有替代声明。例如,下一个不起作用:

Dim mm() As Integer()
Dim mm()() As Integer

PS感谢大家的回复。但我感觉我的问题并不清楚。这与多维数组无关,也与数组的大小无关。这是关于Type的。是的,多数组(Array of Arrays)有很多例子,但它们都使用Variant. 也许(不是)这是创建多阵列的唯一方法?这就是我需要知道的。

4

6 回答 6

1

您可以为 3-D 数组指定如下:

Dim ma(1 To 3, 1 To 2, 0 To 5) as Integer
Dim mb(0 To 2, 0 To 2, 0 To 2) as Integer
于 2013-03-31T11:48:42.303 回答
1

是的,您需要使用变体,至少对于“外部数组”。使用“数组的数组”的好处当然是你可以有参差不齐的数组(子数组可以有不同的 LBound/UBound 值,而不是所有的相同)。

于 2013-04-01T21:34:36.473 回答
0

我认为您必须声明数组的大小,因为内存中的分配..当您将其声明为 Variant 时,编译器在运行时知道类型和大小,因此它不会给您任何错误..但是当您声明它是某种类型..你需要声明大小..看看我在第一条评论中给你的链接

于 2013-03-31T11:27:18.653 回答
0

我在这里找到了一个很好的例子:http: //windowssecrets.com/forums/showthread.php/79979-Array-of-Arrays-(VB6)

Dim ArrayArray() As Variant
ReDim ArrayArray(1)

Dim StringArray() As String
Dim DateArray() As Date

ReDim StringArray(2, 3)
ReDim DateArray(4, 5)

ArrayArray(0) = StringArray
ArrayArray(1) = DateArray

这是一个教程:http: //visualbasic.freetutes.com/learn-vb6/arrays-of-arrays.html

于 2013-03-31T13:02:46.823 回答
0

当你用空()声明它时,你可以稍后用你想要的任何尺寸重新调整它

创建以下项目,运行它,单击 2 个命令按钮,然后查看显示的数据

'1 form with
'    2 command buttons : name=Command1, name=Command2
'    1 label : name=Label1
Option Explicit

Private mintVal() As Integer

Private Sub Command1_Click()
  Dim intIndex1 As Integer, intIndex2 As Integer, intIndex3 As Integer
  ReDim mintVal(5, 3, 4) As Integer
  For intIndex1 = 0 To UBound(mintVal, 1)
    For intIndex2 = 0 To UBound(mintVal, 2)
      For intIndex3 = 0 To UBound(mintVal, 3)
        mintVal(intIndex1, intIndex2, intIndex3) = intIndex1 + intIndex2 + intIndex3
      Next intIndex3
    Next intIndex2
  Next intIndex1
  ShowVals3
End Sub

Private Sub Command2_Click()
  Dim intIndex1 As Integer, intIndex2 As Integer
  ReDim mintVal(3, 7) As Integer
  For intIndex1 = 0 To UBound(mintVal, 1)
    For intIndex2 = 0 To UBound(mintVal, 2)
      mintVal(intIndex1, intIndex2) = intIndex1 * intIndex2
    Next intIndex2
  Next intIndex1
  ShowVals2
End Sub

Private Sub Form_Resize()
  Dim sngWidth As Integer, sngHeight As Single
  Dim sngCmdWidth As Single, sngCmdHeight As Single
  sngWidth = ScaleWidth
  sngCmdHeight = 495
  sngCmdWidth = sngWidth / 2
  sngHeight = ScaleHeight - 495
  Label1.Move 0, 0, sngWidth, sngHeight
  Command1.Move 0, sngHeight, sngCmdWidth, sngCmdHeight
  Command2.Move sngCmdWidth, sngHeight, sngCmdWidth, sngCmdHeight
End Sub

Private Sub ShowVals2()
  Dim intIndex1 As Integer, intIndex2 As Integer
  Dim strShow As String
  strShow = ""
  For intIndex1 = 0 To UBound(mintVal, 1)
    strShow = strShow & CStr(mintVal(intIndex1, 0))
    For intIndex2 = 1 To UBound(mintVal, 2)
      strShow = strShow & "," & CStr(mintVal(intIndex1, intIndex2))
    Next intIndex2
    strShow = strShow & vbCrLf
  Next intIndex1
  Label1.Caption = strShow
End Sub

Private Sub ShowVals3()
  Dim intIndex1 As Integer, intIndex2 As Integer, intIndex3 As Integer
  Dim strShow As String
  strShow = ""
  For intIndex1 = 0 To UBound(mintVal, 1)
    For intIndex2 = 0 To UBound(mintVal, 2)
      strShow = strShow & CStr(mintVal(intIndex1, intIndex2, 0))
      For intIndex3 = 1 To UBound(mintVal, 3)
        strShow = strShow & "," & CStr(mintVal(intIndex1, intIndex2, intIndex3))
      Next intIndex3
      strShow = strShow & vbCrLf
    Next intIndex2
    strShow = strShow & vbCrLf
  Next intIndex1
  Label1.Caption = strShow
End Sub
于 2013-04-02T10:20:05.300 回答
0

另一个使用 udt 的答案,其中包含可以重新调整的数组

Option Explicit

Private Type MyUdt
  strOne() As String
  strTwo() As String
End Type

Private mudtGlobal As MyUdt

Private Sub Form_Click()
  Dim intIndex1 As Integer
  With mudtGlobal
    For intIndex1 = 0 To UBound(.strOne)
      .strOne(intIndex1) = CStr(intIndex1)
    Next intIndex1
  End With 'mudtGlobal
  PrintVals mudtGlobal
End Sub

Private Sub Form_DblClick()
  Dim udtLocal As MyUdt
  'using local udt which is filled with the return value of the function
  udtLocal = FacVals(udtLocal)
  'passing the local udt as an argument
  PrintVals udtLocal
  'passing the global udt as an argument to show the different values
  PrintVals mudtGlobal
End Sub

Private Sub Form_Load()
  With mudtGlobal
    ReDim .strOne(3) As String
    ReDim .strTwo(2, 5) As String
  End With 'mudtGlobal
End Sub

Private Sub PrintVals(udt2Print As MyUdt)
  Dim intIndex1 As Integer, intIndex2 As Integer
  Dim strPrint As String
  strPrint = ""
  With udt2Print
    strPrint = .strOne(0)
    For intIndex1 = 1 To UBound(.strOne)
      strPrint = strPrint & "," & .strOne(intIndex1)
    Next intIndex1
    strPrint = strPrint & vbCrLf & vbCrLf
    For intIndex1 = 0 To UBound(.strTwo, 1)
      strPrint = strPrint & .strTwo(intIndex1, 0)
      For intIndex2 = 1 To UBound(.strTwo, 2)
        strPrint = strPrint & "," & .strTwo(intIndex1, intIndex2)
      Next intIndex2
      strPrint = strPrint & vbCrLf
    Next intIndex1
  End With 'udt2Print
  Print strPrint
End Sub

Private Function FacVals(udtArg As MyUdt) As MyUdt
  Dim intIndex1 As Integer, intIndex2 As Integer
  'copying values
  udtArg = mudtGlobal
  'redimming the local udt
  With udtArg
    ReDim Preserve .strOne(7)
    ReDim Preserve .strTwo(2, 9)
  End With 'udtArg
  With udtArg
    For intIndex1 = 0 To UBound(.strTwo, 1)
      For intIndex2 = 0 To UBound(.strTwo, 2)
        .strTwo(intIndex1, intIndex2) = CStr(intIndex1 * intIndex2)
      Next intIndex2
    Next intIndex1
  End With 'udtarg
  FacVals = udtArg
End Function

双击表格查看结果值

于 2013-04-04T06:08:16.597 回答