我刚刚创建了几个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)