SelfTaughtCodingIdiot
SelfTaughtCodingIdiot

Reputation: 109

Excel 2010 vba array as a class member error

I am working on a project and have run into something that I don't understand. When assigning an array to a class member, the Let and Get names cannot be the same. If they are, I get the error:

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

Can anyone tell me if I'm just doing something wrong, or if this is just how it is. The code below generates the above message.

Test Code:

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 Code:

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

Upvotes: 4

Views: 169

Answers (1)

JustinJDavies
JustinJDavies

Reputation: 2693

Change values() As Variant to values As Variant:

Class Code:

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

Explanation:

values As Variant will be of type Variant, which you later use to store an array. values() As Variant is an array of type Variant, to which an Array cannot be assigned; an Array can only be assigned to the former.

Upvotes: 1

Related Questions