将字典设置为属性

时间:2016-05-14 01:37:25

标签: excel vba excel-vba class

我对VBA并不陌生,但我不熟悉课程,而且我现在正在努力修改一些词典。 这是我班级的缩减版本:

Option Explicit
'Variables required for object operations
Private pFilePath As String         'contains the file path of the TAB file.
...
Private pDATAFields As Object       'dictionary1
Private pMetadata As Object         'dictionary2

Private Sub Class_Initialize()
    Set pDATAFields = CreateObject("Scripting.Dictionary")
    Set pMetadata = CreateObject("Scripting.Dictionary")
End Sub

...

Public Property Get DATAFields() As Object
    DATAFields = pDATAFields
End Property

Public Property Get Metadata() As Object
    Metadata = pMetadata
End Property

'Methods required
Public Sub initialise(sFilePath As String)

    'sTABText contains the text from file at sFilePath
    Dim sTabText As String
    sTabText = harvestTextFile(sFilePath)

    ...

    pTABVersion = pullPart("Version", strArr(1), 1)

    ...

    Set pDATAFields = generateFieldStructureDict(strArr, 6, 5 + pDATAFieldNumber)

    ...

    'Generate the metadata dictionary from the file
    Set pMetadata = generateMetadataDict(strArr, i + 1)
End Sub

Private Function generateFieldStructureDict(arg1 as string, arg2 as integer, arg3 as Integer) as Object
    Dim oObj as Object
    Set oObj = CreateObject("scripting.dictionary")

    ... do stuff

    Set generateFieldStructureDict = oObj
End Function

... Similar function for generateMetadataDict

根据我自己的观察,这应该有效,但由于某种原因,它并不适用。诸如pTABVersion之类的属性设置得很好。它似乎只是pDATAFieldspMetadata未正确设置。

如果我一步一步地遵循函数generateFieldStructureDict,我会在函数结束时看到:

set generateFieldStructureDict = oObj

这部分工作得非常好。 oObj中的字典会传递到generateFieldStructureDict。然而,当我们返回初始化子例程时:

Set pDATAFields = generateFieldStructureDict(strArr, 6, 5 + pDATAFieldNumber)

generateFieldStructureDict似乎丢失了我提供的所有数据。最终clsTab.filePathclsTab.tabVersion将返回pFilePathpTabVersion(因为它们已设置)。但是clsTab.DATAFieldsclsTab.Metadata不会像他们应该的那样返回字典。这是因为pMetadatapDATAFields 由于某种原因未设置

有没有人对我需要做些什么来实现这项工作?

0 个答案:

没有答案