我很难正确设置和调用字符串数组类属性。
当前类属性
Private pNames() As String
'pNames Properties
Public Property Get Names() As String
Names() = pNames
End Property
Public Property Let Names(Names As String)
pNames() = Names
End Property
当前编译错误
Compile Error: Expected array
在下面一行,特别突出显示LBound
:
For Loop1 = LBound(EditCategories.Names) To UBound(EditCategories.Names)
如何将此属性正确注册为字符串数组?
<小时/>我尝试过的
Public Property Let Names()(Names As String)
突出显示空括号并抛出:
Argument required for Property Let or Property Set
Public Property Let Names(Names() As String)
突出显示整行并抛出:
Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalid Set final parameter
最佳答案
支持字段是一个String
数组。类型化数组的 VBA 语法有点奇怪/不一致。
支持字段:
Private pNames() As String
属性:
Public Property Get Names() As String()
注意 String()
括号 - 这在 Private pNames
声明中是非法的 - 想想看。
问题是您无法分配给这样的数组,因此 Property Let
访问器不可能合法 - 这不会编译:
Public Property Let Names(ByRef values As String())
欢迎体验 VBA 中类型化数组的乐趣。只需将其公开为 Variant
并完成即可:
Public Property Get Names() As Variant
Public Property Let Names(ByRef values As Variant)
关于vba - 将字符串数组分配给类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45020999/