Invalid use of vba class property

I have a Student class in VBA (Excel) implemented as follows

Option Explicit

Private name_ As String
Private surname_ As String
Private marks_ As New Collection


Public Property Get getMean() As Single

    Dim sum As Double
    Dim mark As Double
    Dim count As Integer

    For Each mark In marks_
        sum = sum + mark
        count = count + 1
    Next mark

    getMean = sum / count

End Property

Public Property Let setName(name As String)
    name_ = name
End Property

Public Property Get getName() As String
    getName = name_
End Property

Public Property Let setSurname(surname As String)
    surname_ = surname
End Property

Public Property Get getSurname() As String
    getSurname = surname_
End Property

Then I have a main sub where I write:

Dim stud1 As New Student

stud1.setName "Andy"

I got a compilation error on stud1.setName "Andy": Invalid use of property. I do not understand why. Any idea please?

+4
source share
1 answer

Since this property (not a method), you should use the =following values ​​to apply:

Dim stud1 As New Student

stud1.setName = "Andy

BTW, for simplicity you can use the same name for properties getand set:

Public Property Let Name(name As String)
    name_ = name
End Property

Public Property Get Name() As String
    Name = name_
End Property

and then use them as follows:

Dim stud1 As New Student
'set name
stud1.Name = "Andy"
'get name
MsgBox stud1.Name
+5
source

Source: https://habr.com/ru/post/1524874/


All Articles