如何将项目从字典中保存的一组Class对象传递给数组?

时间:2015-06-16 09:02:18

标签: arrays vba class dictionary

我试图通过使用字典(我只是以基本的方式理解)到使用Class对象来保存数据,从而在编码方面取得了一些飞跃。

我创建了一个纯粹的类来保存4个数据项,这些数据需要保存在一起然后再引用。然后我有代码创建了几个这样的Class对象,并将数据填充到它们的4位中。

这是我的课程模块(称为NarrativeGroup):

Private pNarrative As String
Private pBillCat As String
Private pDateIndex As String
Private pFrequency As Long

''''''''''''''''''''''
' Narrative properties
''''''''''''''''''''''
Public Property Get Narrative() As String
    Narrative = pNarrative
End Property

Public Property Let Narrative(Value As String)
    pNarrative = Value
End Property


''''''''''''''''''''''
' BillCat properties
''''''''''''''''''''''
Public Property Get BillCat() As String
    BillCat = pBillCat
End Property

Public Property Let BillCat(Value As String)
    pBillCat = Value
End Property

''''''''''''''''''''''
' DateIndex properties
''''''''''''''''''''''
Public Property Get DateIndex() As String
    DateIndex = pDateIndex
End Property

Public Property Let DateIndex(Value As String)
    pDateIndex = Value
End Property

''''''''''''''''''''''
' Frequency properties
''''''''''''''''''''''
Public Property Get Frequency() As String
    Frequency = pFrequency
End Property

Public Property Let Frequency(Value As String)
    pFrequency = Value
End Property

然后我在正常模块中有以下代码。我试图将4种项目类型加载到一个数组中,然后尝试测试它是否正常工作。我将其加载到数组中的原因是然后使用sub将其输出到工作表中。但代码错误! :

Sub setNarrativeGroup() 'to put the dictionary of Narrative object Items into an array

Set dict_Narratives = New Scripting.Dictionary
dict_Narratives.CompareMode = TextCompare      'make text comparisons so they are not case sensitive

Dim NewNarrative As NarrativeGroup

Set NewNarrative = New NarrativeGroup

Dim array_Narratives As Variant


NewNarrative.Narrative = "fee prep"
NewNarrative.BillCat = "Billing"
NewNarrative.DateIndex = "01.2015"
NewNarrative.Frequency = 3

dict_Narratives.Add NewNarrative.Narrative, NewNarrative

NewNarrative.Narrative = "meeting"
NewNarrative.BillCat = "Trustee Meeting"
NewNarrative.DateIndex = "02.2015"
NewNarrative.Frequency = 1

dict_Narratives.Add NewNarrative.Narrative, NewNarrative

array_Narratives = dict_Narratives.Items

MsgBox array_Narratives(1, 1)

Call PrintArray(array_Narratives, "Sheet1", 1, 1)

End Sub


Sub PrintArray(Data, SheetName As String, intStartRow As Integer, intStartCol As Integer)

    Dim oWorksheet As Worksheet
    Dim rngCopyTo As Range
    Set oWorksheet = ActiveWorkbook.Worksheets(SheetName)

    ' size of array
    Dim intEndRow As Integer
    Dim intEndCol As Integer
    intEndRow = UBound(Data, 1)
    intEndCol = UBound(Data, 2)

    Set rngCopyTo = oWorksheet.Range(oWorksheet.Cells(intStartRow, intStartCol), oWorksheet.Cells(intEndRow, intEndCol))
    rngCopyTo.Value = Data

End Sub

我试图寻找有关使用类对象的字典并将它们分散到数组中的帮助,但似乎没有那么多!任何帮助非常感谢,并为我上面的代码中的任何大禁忌道歉! :)

0 个答案:

没有答案