从 Get 属性引用 Set 属性
我在另一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在上面的代码中,
Set EventButton = mctlEventButton
不会调用以下代码(尝试单步执行代码,您会发现它不会单步执行Set
)另一方面,this 作为一个用于返回值的语句。
实际上,可以将
Get
视为以属性形式包装,供开发人员执行获取/设置操作。
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 intoSet
)On the other hand, this of it as a statement that is used to return a value.
Effectively, think of
Get
aswrapped in form of a property for developers to do get/set.