很抱歉新手问题,但是目前如果我需要多阵列(阵列数组),我将其声明为Variant:
Dim ma() As Variant
ReDim ma(1 To 3)
ma(1) = Array(1, 2, 3)
除了它有效之外,我想知道是否有另类声明。 例如,接下来没有工作:
Dim mm() As Integer()
Dim mm()() As Integer
P.S。感谢大家的回应。但我觉得我的问题不明确。它不是关于多维数组,也不关于数组的大小。这是关于类型。是的,多阵列(阵列阵列)有很多例子,但他们都使用Variant
。也许(比如ot not)这是创建多阵列的唯一方法吗?这就是我需要知道的。
答案 0 :(得分: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
答案 1 :(得分:1)
是的,你需要使用Variants,至少对于“外部阵列”。使用“数组数组”的好处当然是你可以使用不规则的数组(子数组可以有不同的LBound / UBound值,而不是所有的相同)。
答案 2 :(得分:0)
我认为你必须声明数组的大小,因为内存中的分配。 当你将它声明为Variant时,编译器会在运行时知道类型和大小,因此它不会给你任何错误..但是当你将它声明为某种类型时......你需要声明大小..看看链接我在第一条评论中给了你
答案 3 :(得分: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
答案 4 :(得分:0)
当您使用empty()声明它时,您可以稍后使用您想要的任何尺寸重新编辑它
创建以下项目,运行它,单击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
答案 5 :(得分: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
双击表单以查看结果值