【问题标题】:Declare multi-array声明多数组
【发布时间】:2013-03-31 11:19:20
【问题描述】:

很抱歉这个菜鸟问题,但目前如果我需要多数组(Array of Arrays),我将其声明为 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.感谢大家的回复。但我感觉我的问题并不清楚。这与多维数组无关,也与数组的大小无关。这是关于类型。是的,多数组(Array of Arrays)有很多示例,但它们都使用Variant。也许(不是)这是创建多阵列的唯一方法?这就是我需要知道的全部内容。

【问题讨论】:

  • 是的,您需要使用变体,至少对于“外部数组”而言。使用“数组的数组”的好处当然是你可以有参差不齐的数组(子数组可以有不同的 LBound/UBound 值,而不是所有的相同)。
  • 如果您需要“列”的特定类型,请尝试 UDT 数组。
  • @panayot - 我在下面的回答不使用变体。
  • @chuex - 如果问题是关于多维数组,你的建议很好。

标签: arrays syntax vb6 declaration


【解决方案1】:

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

【讨论】:

  • 您不必使用变体。在下面查看我的答案
【解决方案2】:

您可以为 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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我在这里找到了一个很好的例子: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

      【讨论】:

        【解决方案5】:

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

        创建以下项目,运行它,单击 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
        

        【讨论】:

        • 不过,这对“参差不齐”的数组没有任何帮助,这可能是他在问题中所追求的。数组的数组与多维数组完全不同。
        • 确实如此。在这种情况下,我将创建一个包含内部数组的用户定义类型(可以为每个元素重新调整)。
        • 你可以,但是你有一个不能作为参数传递、作为返回值接收、存储在集合中等的混乱,而无需创建单独的 DLL 来定义它。
        • 为什么不呢?我可能不理解你,因为你似乎比我更有经验:) 但我经常将 udt 作为参数和返回值传递。我从来没有在收藏中使用过它们,所以不确定。您定义以在正确的范围内定义 udt。许多 api 也需要特殊的 udt。我使用 udt 添加了另一个答案来说明我的意思
        【解决方案6】:

        另一个使用 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
        

        双击表格查看结果值

        【讨论】:

        • 我忘记创建一个 udt 数组来制作 strOne 和 strTwo 内部数组,抱歉
        • 非常有用(+1),我会玩这个。
        猜你喜欢
        • 1970-01-01
        • 2014-11-20
        • 1970-01-01
        • 1970-01-01
        • 2023-02-09
        • 2020-10-24
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        相关资源
        最近更新 更多