首页 > 解决方案 > 具有“整数”类型键和“整数动态数组”类型值的字典:类型不匹配错误

问题描述

我想要一个字典,其键类型为“整数”,值类型为“动态整数数组”。

以下代码给了我一个类型不匹配。

第一条if语句设置了一个 key=0 和数组 [0] 的字典。
第二条if语句尝试将键修改为数组 [0,1] 的值。

Sub tester()
    
    ' prepare graph adjacency dictionary
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
        
    Dim a() As Integer
    Dim ub As Integer
        
    If (Not dict.Exists(0)) Then
        ReDim a(0 To 0)
        a(0) = 0
        dict.Add 0, a(0)
    End If
            
    If (dict.Exists(0)) Then
        ub = UBound(dict(0)) ' type mismatch here ? why ?
        ReDim a(0 To ub)
        a = dict(0)
        ReDim Preserve a(0 To ub + 1)
        a(ub + 1) = 1
        dict(0) = a
    End If
      
End Sub

标签: excelvba

解决方案


推荐阅读