VB6中Property Set和Property Let有什么区别?

发布于 2024-10-18 05:02:25 字数 219 浏览 1 评论 0原文

我刚刚创建了几个 Property Set 方法,但它们未编译。当我将它们更改为 Property Let 时,一切都很好。

此后,我研究了文档以找出 Property SetProperty Let 之间的区别,但必须承认我一点也不明智。有什么区别吗?如果有的话有人可以提供一个正确解释的指针吗?

I have just created several Property Set methods, and they didn't compile. When I changed them to Property Let, everything was fine.

I have since studied the documentation to find the difference between Property Set and Property Let, but must admit to being none the wiser. Is there any difference, and if so could someone offer a pointer to a proper explanation of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日暮斜阳 2024-10-25 05:02:25

Property Set 用于对象(例如,类实例)

Property Let 用于“普通”数据类型(例如,字符串、布尔值、长整型等)

Property Set is for objects (e.g., class instances)

Property Let is for "normal" datatypes (e.g., string, boolean, long, etc.)

林空鹿饮溪 2024-10-25 05:02:25

Property LetProperty 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,从而导致不带 Set< 的不寻常语法 oObj.Object = MyObject /代码> 声明。在这种情况下,使用 Set 语句会导致编译时或运行时错误,因为 oObj 类上没有实现 Property Set Object

我倾向于为标准类型(字体、图片等)的属性实现 Property SetProperty Let,但具有不同的语义。通常在 Property Let 上,我倾向于执行“深度复制”,即克隆 StdFont 而不是仅仅保存对原始对象的引用。

Property Let is more versatile than Property Set. The latter is restricted to object references only. If you have this property in a class

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

You can call Property Set Picture with

Set oObj.Picture = Me.Picture

You can call Property Let Picture with both

Let oObj.Picture = Me.Picture
oObj.Picture = Me.Picture

Implementing Property Set is what other developers expect for properties that are object references but sometimes even Microsoft provide only Property Let for reference properties, leading to the unusual syntax oObj.Object = MyObject without Set statement. In this case using Set statement leads to compile-time or run-time error because there is no Property Set Object implemented on oObj class.

I tend to implement both Property Set and Property Let for properties of standard types -- fonts, pictures, etc -- but with different semantics. Usually on Property Let I tend to perform "deep copy", i.e. cloning the StdFont instead of just holding a reference to the original object.

绳情 2024-10-25 05:02:25

Property Set 用于类对象变量 (ByRef),而 Property Let 用于类值变量 (ByVal)

Property Set is for object-like variables (ByRef) whereas Property Let is for value-like variables (ByVal)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文