vba Dim、Global、Public 和 Private 作为模块化字段访问修饰符有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3815547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?
提问by Ben McCormack
In VB6/VBA, you can declare module-level variables outside of a specific Subor Functionmethod. I've used Privateand Publicbefore inside modules and understand them like so:
在 VB6/VBA 中,您可以在特定方法Sub或Function方法之外声明模块级变量。我在内部模块中使用过Private和Public之前,并像这样理解它们:
Public- visible to all code inside the module and all code outside the module, essentially making it global.Private- visible only to code inside the module.
Public- 对模块内的所有代码和模块外的所有代码可见,本质上使其成为全局的。Private- 仅对模块内的代码可见。
I've noticed that you can use Dimand Globalas modifiers for modular variables. Are Dimand Globaldifferent from Privateand Public, respectively, when used as access modifiers on modular fields? If so, how are they different?
我注意到您可以使用Dim和Global作为模块化变量的修饰符。是Dim与Global从不同Private,并Public分别作为模块化领域访问修饰符使用时?如果有,它们有什么不同?
回答by Joe Jordan
Dimand Privatework the same, though the common convention is to use Privateat the module level, and Dimat the Sub/Function level. Publicand Globalare nearly identical in their function, however Globalcan only be used in standard modules, whereas Publiccan be used in all contexts (modules, classes, controls, forms etc.) Globalcomes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public.
Dim并且Private工作相同,尽管通用约定是Private在模块级别和Dim子/功能级别使用。Public并且Global它们的功能几乎相同,但是Global只能在标准模块中使用,而Public可以在所有上下文(模块、类、控件、表单等)中使用Global来自较旧版本的 VB 并且可能保持向后兼容性,但是已被 完全取代Public。

