VBA 类中的字典属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1539986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:52:08  来源:igfitidea点击:

Dictionary Property in VBA Class

excelvbadictionary

提问by Irwin M. Fletcher

I have been asked to modify an Excel sheet with some arcaic programming. I have decided to rewrite it rather then modify all of the many GOTO statments and static arrays. My background is in C# so it has been a bit of a challenge (note: I am sure the naming convention is bad, I am used to being able to use underscore to define private variables)

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

I am having trouble inializing an attribute of the type dictionary within a class that I have in a VBA application.

我在初始化 VBA 应用程序中的类中的类型字典的属性时遇到问题。

The shortened version of the class looks like this

该类的缩短版本如下所示

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

When I try to use the following I get the error "Argument not optional" within the Get property of the headCount() attribute.

当我尝试使用以下内容时,我在 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

I assume somewhere I need to inialize the dictionary (i.e. = New Dictionary) however I am strugling with where to place it. In C# I do this in the constructor without issue, not sure what to do here.

我假设我需要在某个地方初始化字典(即 = 新字典),但是我正在为将它放在哪里而苦苦挣扎。在 C# 中,我在构造函数中执行此操作没有问题,不知道在这里做什么。

Thanks

谢谢

回答by Adam Ralph

You can do it in the constructor of the VBA class, like so:-

您可以在 VBA 类的构造函数中执行此操作,如下所示:-

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

Don't forget to always use the Setkeyword when assigning an object reference, e.g.:-

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

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