我刚刚创建了几个Property Set
方法,但它们没有编译。当我将它们更改为 时Property Let
,一切都很好。
从那以后,我研究了文档以找出 和 之间的区别Property Set
,Property Let
但必须承认自己并不明智。有什么区别吗,如果有的话,有人可以提供一个正确解释的指针吗?
我刚刚创建了几个Property Set
方法,但它们没有编译。当我将它们更改为 时Property Let
,一切都很好。
从那以后,我研究了文档以找出 和 之间的区别Property Set
,Property Let
但必须承认自己并不明智。有什么区别吗,如果有的话,有人可以提供一个正确解释的指针吗?
Property Set
用于对象(例如,类实例)
Property Let
适用于“普通”数据类型(例如,字符串、布尔值、长整数等)
Property Let
比Property Set
. 后者仅限于对象引用。如果您在班级中有此属性
Private m_oPicture As StdPicture
Property Get Picture() As StdPicture
Set Picture = m_oPicture
End Property
Property Set Picture(oValue As StdPicture)
Set m_oPicture = oValue
End Property
Property Let Picture(oValue As StdPicture)
Set m_oPicture = oValue
End Property
你可以打电话Property Set Picture
给
Set oObj.Picture = Me.Picture
您可以同时Property Let Picture
调用
Let oObj.Picture = Me.Picture
oObj.Picture = Me.Picture
实现Property Set
是其他开发人员对作为对象引用的属性的期望,但有时甚至 Microsoft 只Property Let
提供引用属性,导致不寻常的语法oObj.Object = MyObject
没有Set
语句。在这种情况下 usingSet
语句会导致编译时或运行时错误,因为类上没有Property Set Object
实现oObj
。
我倾向于同时实现Property Set
标准Property Let
类型的属性——字体、图片等——但具有不同的语义。通常Property Let
我倾向于执行“深拷贝”,即克隆StdFont
而不是仅仅持有对原始对象的引用。
Property Set
用于类对象变量 (ByRef) 而Property Let
用于类值变量 (ByVal)