【发布时间】:2019-07-31 17:40:49
【问题描述】:
我在 VBA (Excel) 中的 Student 类实现如下
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" 出现编译错误:属性使用无效。
我不明白为什么。请问有什么想法吗?
【问题讨论】:
-
在接受的答案之上;
GetMean()对我来说似乎无效;marks在任何地方都没有标注尺寸,除非它是一个错字,因为您在_末尾缺少下划线。每个循环中的迭代器也必须是变体类型 - 你有一个双精度。如果 setter 和 getter 都是公共的,那么拥有公共属性的意义何在?而不是有两个属性只是维度name和surname作为公共变量,VBA会将它们视为一个类'properties -
你是正确的我。对于
getmean(),这是一个错字。公开name和 `surname.这仅用于学术目的。
标签: vba excel properties