从 Get 属性引用 Set 属性

发布于 2024-11-10 20:09:35 字数 718 浏览 2 评论 0原文

我在另一个 SO 问题中发现了这段 vba 代码。在类的Get属性中引用Set属性有什么意义吗?

私有WithEvents mctlEventButton作为MSForms.CommandButton

公共属性设置EventButton(ctlButton As MSForms.CommandButton)
设置 mctlEventButton = ctlButton
End Property

为什么要这样做? ...

公共属性 Get EventButton() As MSForms.CommandButton
设置EventButton = mctlEventButton
End Property

该代码展示了如何使用集合来迭代一组控件。问题实际上并不是关于代码的那部分,因此问题中没有解决它。使用问题中的示例,我遇到了此处发布的问题,因为“获取属性”正在使用“设置”属性。那么什么时候会有用呢?

这是SO问题的链接:VBA Excel中的对象数组或集合

I came across this bit of vba code posted in another SO question. Is there any significance in referencing the Set property in the Get Property of a class?

Private WithEvents mctlEventButton As MSForms.CommandButton

Public Property Set EventButton(ctlButton As MSForms.CommandButton)
Set mctlEventButton = ctlButton
End Property

Why do this? ...

Public Property Get EventButton() As MSForms.CommandButton
Set EventButton = mctlEventButton
End Property

The code was showing how to use collections to iterate through a group of controls. The question wasnt really about that part of the code, so it wasnt addressed in the question. Using the example from the question, I ran into an issue with the bit I posted here because the Get Property was using the Set property. So when would that be useful?

Here is the link to the SO Question: object array or collection in VBA Excel

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

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

发布评论

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

评论(1

无语# 2024-11-17 20:09:35
Public Property Get EventButton() As MSForms.CommandButton
   Set EventButton = mctlEventButton
End Property

在上面的代码中,Set EventButton = mctlEventButton 不会调用以下代码(尝试单步执行代码,您会发现它不会单步执行Set)

Public Property Set EventButton(ctlButton As MSForms.CommandButton)
   Set mctlEventButton = ctlButton
End Property

另一方面,this 作为一个用于返回值的语句。
实际上,可以将 Get 视为

Public function EventButton() As MSForms.CommandButton
   Set EventButton = mctlEventButton  'returning the value from the function
End Property

以属性形式包装,供开发人员执行获取/设置操作。

Public Property Get EventButton() As MSForms.CommandButton
   Set EventButton = mctlEventButton
End Property

In the code above, Set EventButton = mctlEventButton won't call the following (try stepping through the code & you will see that it doesn't step into Set)

Public Property Set EventButton(ctlButton As MSForms.CommandButton)
   Set mctlEventButton = ctlButton
End Property

On the other hand, this of it as a statement that is used to return a value.
Effectively, think of Get as

Public function EventButton() As MSForms.CommandButton
   Set EventButton = mctlEventButton  'returning the value from the function
End Property

wrapped in form of a property for developers to do get/set.

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