声明上下文和默认访问级别 (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
Variable (Dim 语句Variable (Dim Statement) 不允许Not allowed PrivatePublic Structure,但 Interface中不允许)Private (Public in Structure, not allowed in Interface) Public
常量(Const 语句Constant (Const Statement) 不允许Not allowed PrivatePublic Structure,但 Interface中不允许)Private (Public in Structure, not allowed in Interface) Public
枚举(Enum 语句Enumeration (Enum Statement) Friend Public 不允许Not allowed
Class (Class 语句Class (Class Statement) Friend Public 不允许Not allowed
Structure (结构语句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 (Function 语句Sub 语句Procedure (Function Statement, Sub Statement) 不允许Not allowed Public 不允许Not allowed
外部引用(Declare 语句External reference (Declare Statement) 不允许Not allowed PublicInterface不允许)Public (not allowed in Interface) 不允许Not allowed
运算符(Operator 语句Operator (Operator Statement) 不允许Not allowed Public (不允许 InterfaceModulePublic (not allowed in Interface or Module) 不允许Not allowed
属性(Property 语句Property (Property Statement) 不允许Not allowed Public 不允许Not allowed
Default 属性(默认值Default property (Default) 不允许Not allowed PublicModule不允许)Public (not allowed in Module) 不允许Not allowed
事件(事件语句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