属性VBA类的使用无效



我在vba(excel)中实现的学生课程如下

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

然后我有一个主要子,我写的地方:

Dim stud1 As New Student
stud1.setName "Andy"

我在stud1.setName "Andy"上遇到了编译错误:属性的使用无效。我不明白为什么。有什么想法,请?

,因为它是属性(不是方法),您应该使用 =应用一个值:

Dim stud1 As New Student
stud1.setName = "Andy"

btw,为简单起见,您可以使用getset属性的相同名称:

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

然后使用它们如下:

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

最新更新