2009-10-08 53 views
1

我被要求用一些arcaic编程修改Excel工作表。我决定重写它,而不是修改所有的GOTO语句和静态数组。我的背景是在C#中,所以它一直是一个挑战(注意:我确定命名约定是坏的,我习惯于能够使用下划线来定义私有变量)VBA中的字典属性

我遇到问题了inializing an我在VBA应用程序中的类中的类型字典的属性。

类的缩短版看起来像这样

Private pTerminalCode As String 
Private pTerminalName As String 
...... other attributes 
Private pPayRoll As Dictionary 

'Propeties 
Public Property Get terminalCode() As String 
    terminalCode = pTerminalCode 
End Property 
Public Property Let terminalCode(Value As String) 
    pTerminalCode = Value 
End Property 
....... more properties 
Public Property Get headCount() As Dictionary 
    headCount = pHeadCount 
End Property 
Public Property Let headCount(Value As Dictionary) 
    pHeadCount = Value 
End Property 

当我尝试使用下面我得到的错误“参数不可选”的人数()属性的获取属性中。

Private Function PopulateTerminal() 
    Dim terminal As clsTerminal 
    Set terminal = New clsTerminal 

    terminal.terminalCode = "Wil" 
    terminal.headCount.Add "Company", 100 
End Function 

我假设某处我需要inialize字典(即=新词典),但我正在努力与它放置在哪里。在C#中,我在构造函数中做这个没有问题,不知道在这里做什么。

感谢

回答

3

你可以做到这一点在VBA类的构造函数,像这样: -

Public Sub Class_Initialize() 
    Set myDictionary = New Dictionary 
End Sub 

分配的对象引用时,不要忘记常使用Set关键字,例如: -

Public Property Get Foo() As Dictionary 
    Set Foo = myDictionary 
End Sub 
+0

集是一个大发现,就是这里的“参数不可选”错误是由正在添加,但是,我现在receieving“对象变量或带块变量未设置” – 2009-10-08 20:01:15

+0

明白了。不知道为什么,但如果我在构造函数中初始化,我得到对象变量错误,但是如果我在声明它的工作原理inialize。感谢您的帮助 – 2009-10-08 20:04:14

+0

啊是的,在声明中初始化是另一种选择。也许这更好,因为它会导致延迟初始化。 – 2009-10-08 20:36:12