作为模块化字段访问修饰符的 Dim、Global、Public 和 Private 有什么区别? [英] What is the difference between Dim, Global, Public, and Private as Modular Field Access Modifiers?
问题描述
在 VB6/VBA 中,您可以在特定的 Sub
或 Function
方法之外声明模块级变量.我在模块内部使用过 Private
和 Public
并像这样理解它们:
In VB6/VBA, you can declare module-level variables outside of a specific Sub
or Function
method. I've used Private
and Public
before inside modules and understand them like so:
Public
- 对模块内的所有代码和模块外的所有代码可见,本质上使其成为全局的.Private
- 仅对模块内的代码可见.
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.
我注意到您可以使用 Dim
和 Global
作为模块化变量的修饰符.当用作模块化字段的访问修饰符时,Dim
和 Global
是否分别不同于 Private
和 Public
?如果有,它们有何不同?
I've noticed that you can use Dim
and Global
as modifiers for modular variables. Are Dim
and Global
different from Private
and Public
, respectively, when used as access modifiers on modular fields? If so, how are they different?
推荐答案
Dim
和 Private
的工作方式相同,但常见的约定是使用 Private
在模块级别,Dim
在子/功能级别.Public
和 Global
在功能上几乎相同,但是 Global
只能在标准模块中使用,而 Public
可以在所有上下文(模块、类、控件、表单等)中使用.Global
来自旧版本的 VB,可能为了向后兼容而保留,但已被 Public<完全取代/代码>.
Dim
and Private
work the same, though the common convention is to use Private
at the module level, and Dim
at the Sub/Function level. Public
and Global
are nearly identical in their function, however Global
can only be used in standard modules, whereas Public
can be used in all contexts (modules, classes, controls, forms etc.) Global
comes from older versions of VB and was likely kept for backwards compatibility, but has been wholly superseded by Public
.
这篇关于作为模块化字段访问修饰符的 Dim、Global、Public 和 Private 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!