Excel 2010 vba数组作为类成员错误
我正在做一个项目,遇到了一些我不明白的东西。 给一个类成员分配一个数组时, Let
和Get
名字不能相同。 如果是,我得到的错误是:
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
任何人都可以告诉我,如果我只是做错了什么,或者如果这是怎么回事。 下面的代码生成上面的消息。
testing代码:
Sub loadServer() Dim testServer As AvayaServer Dim i As Long Dim arr() As Variant arr = Array("1", "2", "3", "4", "5") Set testServer = New AvayaServer testServer.Name = "This Sucks" testServer.Skill = arr MsgBox testServer.Skills(4) MsgBox testServer.Name End Sub
class级代码:
Private pName As String Private pSkills() As String Public Property Get Skills() As Variant Skills = pSkills() End Property Public Property Let Skills(values() As Variant) ReDim pSkills(UBound(values)) Dim i As Long For i = LBound(values) To UBound(values) pSkills(i) = values(i) Next End Property
将values() As Variant
更改为values As Variant
:
class级代码:
Private pName As String Private pSkills() As String Public Property Get Skills() As Variant Skills = pSkills() End Property Public Property Let Skills(values As Variant) 'Fixed here ReDim pSkills(UBound(values)) Dim i As Long For i = LBound(values) To UBound(values) pSkills(i) = values(i) Next End Property
说明:
values As Variant
将是typesVariant
,您稍后将使用它来存储数组。 values() As Variant
是values() As Variant
types的数组, Array
不能赋值给它; Array
只能分配给前者。