声明上下文和默认访问级别 (Visual Basic)Declaration Contexts and Default Access Levels (Visual Basic)

本主题介绍了哪些 Visual Basic 类型可以在哪些其他类型中进行声明,如果未指定,它们的访问级别将默认为。This topic describes which Visual Basic types can be declared within which other types, and what their access levels default to if not specified.

声明上下文级别Declaration Context Levels

编程元素的 声明上下文 是声明它的代码区域。The declaration context of a programming element is the region of code in which it is declared. 这通常是另一个编程元素,该元素称为 " 包含元素"。This is often another programming element, which is then called the containing element.

声明上下文的级别如下:The levels for declaration contexts are the following:

  • 命名空间级别 -在源文件或命名空间中,但不在类、结构、模块或接口中Namespace level — within a source file or namespace but not within a class, structure, module, or interface

  • 模块级别 -在类、结构、模块或接口中,但不在过程或块中Module level — within a class, structure, module, or interface but not within a procedure or block

  • 过程级别 -在过程或块中 (如 IfFor) Procedure level — within a procedure or block (such as If or For)

下表显示了各种已声明的编程元素的默认访问级别,具体取决于它们的声明上下文。The following table shows the default access levels for various declared programming elements, depending on their declaration contexts.

已声明的元素Declared element 命名空间级别Namespace level 模块级别Module level 过程级别Procedure level
变量 (Dim 语句) Variable (Dim Statement) 不允许Not allowed Private (Public 在中 Structure ,不允许在) 中使用 InterfacePrivate (Public in Structure, not allowed in Interface) Public
常量 (Const 语句) Constant (Const Statement) 不允许Not allowed Private (Public 在中 Structure ,不允许在) 中使用 InterfacePrivate (Public in Structure, not allowed in Interface) Public
枚举 (枚举语句) Enumeration (Enum Statement) Friend Public 不允许Not allowed
语句 (类) Class (Class Statement) Friend Public 不允许Not allowed
结构 (结构语句) Structure (Structure Statement) Friend Public 不允许Not allowed
Module (Module 语句) Module (Module Statement) Friend 不允许Not allowed 不允许Not allowed
Interface (Interface 语句) Interface (Interface Statement) Friend Public 不允许Not allowed
Procedure (函数语句Sub 语句) Procedure (Function Statement, Sub Statement) 不允许Not allowed Public 不允许Not allowed
外部引用 (声明语句) External reference (Declare Statement) 不允许Not allowed Public不允许在) 中 (InterfacePublic (not allowed in Interface) 不允许Not allowed
运算符 (运算符语句) Operator (Operator Statement) 不允许Not allowed Public``Interface或) 不允许 (ModulePublic (not allowed in Interface or Module) 不允许Not allowed
Property (属性语句) Property (Property Statement) 不允许Not allowed Public 不允许Not allowed
默认属性 (默认属性) Default property (Default) 不允许Not allowed Public不允许在) 中 (ModulePublic (not allowed in Module) 不允许Not allowed
Event (事件语句) Event (Event Statement) 不允许Not allowed Public 不允许Not allowed
委托 (委托语句) Delegate (Delegate Statement) Friend Public 不允许Not allowed

有关详细信息,请参阅 Visual Basic 中的访问级别For more information, see Access levels in Visual Basic.

请参阅See also