我正在开展一个项目并遇到了一些我不了解的事情。将数组分配给类成员时,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
任何人都可以告诉我,我是做错了什么,或者这是不是就是这样。以下代码生成上述消息。
测试代码:
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
答案 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 Variant
是Variant
类型的数组,无法为其分配Array
; Array
只能分配给前者。