VBA类中的Dictionary属性

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

我在使用VBA应用程序的类中无法使用types字典的属性。

这个类的缩短版本看起来像这样

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 

当我尝试使用下面的代码时,在headCount()属性的Get属性中出现错误“Argument not optional”。

 Private Function PopulateTerminal() Dim terminal As clsTerminal Set terminal = New clsTerminal terminal.terminalCode = "Wil" terminal.headCount.Add "Company", 100 End Function 

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

谢谢

你可以在VBA类的构造函数中完成它,如下所示:

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

分配对象引用时不要忘记始终使用Set关键字,例如: –

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