Excel 2010 vba数组作为类成员错误

时间:2014-06-27 16:28:53

标签: excel excel-vba vba

我正在开展一个项目并遇到了一些我不了解的事情。将数组分配给类成员时,LetGet名称不能相同。如果是,我得到错误:

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

任何人都可以告诉我,我是做错了什么,或者这是不是就是这样。以下代码生成上述消息。

测试代码:

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

班级代码:

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

1 个答案:

答案 0 :(得分:1)

values() As Variant更改为values As Variant

课程代码:

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的类型为Variant,您稍后会用它来存储数组。 values() As VariantVariant类型的数组,无法为其分配Array; Array只能分配给前者。