VBA - 用一类词典填充数组

时间:2015-05-20 21:03:59

标签: vba

我创建了一类字典对象。然后我初始化类并将三个空白副本放入一个数组中。然后我继续填写数组中的一些字典值。这种行为不符合我的预期。

运行代码后我希望数组填充这样  VersionAry(1)在每个字典中保存一个项目,VersionAry(2)在每个字典中保存一个项目,VersionAry(3)保持为空。

相反,每个数组元素都包含相同的两个项目。

看起来像这一行:

VersionAry(1).dictDate.Add 1, 201501

实际上将1,201501添加到VersionAry(1),VersionAry(2)和VersionAry(3)中的dictDate字典中,而不仅仅是作为我的目标的VersionAry(1)。

班级代码:

'Class Module named: cDailyData

Public dictDate As Object
Public dictTotal As Object
Public dictFees As Object

Public Sub Init()
    Set dictDate = CreateObject("Scripting.Dictionary")
    Set dictTotal = CreateObject("Scripting.Dictionary")
    Set dictFees = CreateObject("Scripting.Dictionary")
End Sub

模块代码:

'Module Code
Sub ModuleCode2()
Dim clsDailyResults As cDailyData

Dim VersionAry() As Variant
ReDim VersionAry(1 To 3)

Set clsDailyResults = New cDailyData
clsDailyResults.Init

For i = 1 To 3
    Set VersionAry(i) = clsDailyResults
Next i

VersionAry(1).dictDate.Add 1, 201501
VersionAry(1).dictFees.Add 1, 100
VersionAry(1).dictTotal.Add 1, 1000

VersionAry(2).dictDate.Add 2, 201501
VersionAry(2).dictFees.Add 2, 200
VersionAry(2).dictTotal.Add 2, 2000

End Sub

2 个答案:

答案 0 :(得分:0)

您将多个相同的对象添加到数组中。字典已经包含您的索引,因此您可以直接访问它们。如果您为Date,Fees和Total提供属性,然后将 cDailyData 的多个实例存储在字典或数组中,则Class将更有用。该课应该看起来更像这样:

'Class Module named: cDailyData
Option Explicit

Private m_date As Long
Private m_fees As Long
Private m_total As Long

Public Property Let Day(in_value As Long)
    m_date = in_value
End Property

Public Property Get Day() As Long
    Day = m_date
End Property

Public Property Let Total(in_value As Long)
    m_total = in_value
End Property

Public Property Get Total() As Long
    Total = m_total
End Property

Public Property Let Fees(in_value As Long)
    m_fees = in_value
End Property

Public Property Get Fees() As Long
    Fees = m_fees
End Property

如果您的密钥是非顺序且唯一的,则字典将是合适的,模块将如下所示:

Sub ModuleCode2()

    Dim VersionDict As New Dictionary
    Dim result As New cDailyData

    result.Day = 201501
    result.Fees = 100
    result.Total = 1000

    VersionDict.Add 1, result

    Set result = New cDailyData
    result.Day = 201501
    result.Fees = 200
    result.Total = 2000

    VersionDict.Add 2, result

    Dim key As Variant
    For Each key In VersionDict.Keys
        Debug.Print VersionDict(key).Fees
    Next key

End Sub

如果您不关心密钥是什么,只需要一个cDailyData数组,那就更像是这样:

Sub ModuleCode2a()
    Dim VersionAry() As cDailyData
    ReDim VersionAry(1 To 3)

    Dim i As Integer
    For i = 1 To 3
        Set VersionAry(i) = New cDailyData
    Next i

    VersionAry(1).Day = 201501
    VersionAry(1).Fees = 100
    VersionAry(1).Total = 1000

    VersionAry(2).Day = 201501
    VersionAry(2).Fees = 200
    VersionAry(2).Total = 2000

    For i = 1 To 3
        Debug.Print VersionAry(i).Fees
    Next i
End Sub

答案 1 :(得分:0)

我每次都更改代码以创建一个新类,这似乎有效

For i = 1 To 3
    Set clsDailyResults = New cDailyData
    clsDailyResults.Init
    Set VersionAry(i) = clsDailyResults
Next i

而不是

Set clsDailyResults = New cDailyData
clsDailyResults.Init

For i = 1 To 3
    Set VersionAry(i) = clsDailyResults
Next i