问题描述
在VBA中使用类模块时,如果使用
例如,下面的代码(
一切都很好。当然,当一个类模块从它自己内部调用时,这个子是公共还是私有?
我意识到我可以调用 Private Sub 不使用 Me ,但我想了解为什么VBA以这种方式行为。感谢。
Private Sub Class_Initialize()
Me.QuoteID = 15
Call Me.SetupQuote
End Sub
Private Sub SetupQuote()
Set Quote = New classQuote
Call Quote.SetQuote(Me.QuoteID)
End Sub
这不会是一个非常令人满意的答案, Me界面。这可能不是一个特定的选择,只有显示面向公众的属性和方法。显然,更好的用户体验将是公开和公开。
我的界面可能说确定我是什么样的对象,创建一个实例对象,并公开该对象实例。
当'Me'创建时,没有暴露私有内容的机制。对象实例只显示公共内容,就像在另一个模块中创建和对象实例一样。一个实例,当你创建一个实例的时候。如果他们想这样做,他们将不得不通过一些标志到类构建器,指示它是通过Me界面或其他东西构建。如果标志设置,暴露私人的东西,否则不。即使他们想到了,他们可能不会实现它。这是额外的复杂性,有人会提出,你可以只键入过程名称。
我不为MS工作或有任何特殊的知识。这只是我的猜测,为什么一个显然有用的功能不存在。
When using a Class Module in VBA, how come a call to a Private Sub will fail when using Me in front of it, but a call to a Public Sub is OK?
For example, the code below (not a full Class Module, just a snippit) produces the error Method or data member not found or the line Call Me.SetupQuote.
However, if I make SetupQuote() a Public Sub all is fine. Surely when a Class Module is calling from within itself it shouldn't matter if the sub is public or private?
I realise I can just call a Private Sub without using Me, but I want to understand why VBA behaves in this way. Thanks.
Private Sub Class_Initialize() Me.QuoteID = 15 Call Me.SetupQuote End Sub Private Sub SetupQuote() Set Quote = New classQuote Call Quote.SetQuote(Me.QuoteID) End Sub
This won't be a very satisfying answer, but that's the way MS programmed the Me interface. It probably wasn't a specific choice someone made to only show publicly facing properties and methods. Clearly the better user experience would be to expose both private and public. Rather it was just a side-effect of how Me was implemented.
The Me interface probably says "determine what kind of object I'm in, create an instance of that object, and expose that object instance." That object instance will only show public stuff just like it would if you had created and object instance in another module.
There's no mechanism for exposing private stuff when 'Me' creates an instance any more than there is when you create an instance. If they wanted to do it, they would have to pass some flag into the class builder that indicated whether it was being built via the Me interface or something else. If the flag was set, expose the private stuff, otherwise don't. Even if they had thought of that, they probably wouldn't implement it. It's additional complexity and someone would have brought up the point that you can just type the procedure name.
I don't work for MS or have any special knowledge about this. It's just my guess about why an obviously useful feature isn't there.
这篇关于为什么使用`Me'时调用Class Module Private Subs失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!