共用方式為


Declaration Contexts and Default Access Levels 

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

  • 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 Statement (Visual Basic))

Not allowed

Private (Public in Structure, not allowed in Interface)

Public

Constant (Const Statement (Visual Basic))

Not allowed

Private (Public in Structure, not allowed in Interface)

Public

Enumeration (Enum Statement (Visual Basic))

Friend

Public

Not allowed

Class (Class Statement (Visual Basic))

Friend

Public

Not allowed

Structure (Structure Statement)

Friend

Public

Not allowed

Module (Module Statement)

Friend

Not allowed

Not allowed

Interface (Interface Statement (Visual Basic))

Friend

Public

Not allowed

Procedure (Function Statement (Visual Basic), Sub Statement (Visual Basic))

Not allowed

Public

Not allowed

External reference (Declare Statement)

Not allowed

Public (not allowed in Interface)

Not allowed

Operator (Operator Statement)

Not allowed

Public (not allowed in Interface or Module)

Not allowed

Property (Property Statement)

Not allowed

Public

Not allowed

Default property (Default (Visual Basic))

Not allowed

Public (not allowed in Module)

Not allowed

Event (Event Statement)

Not allowed

Public

Not allowed

Delegate (Delegate Statement)

Friend

Public

Not allowed

For more information, see Access Levels in Visual Basic.

See Also

Reference

Friend (Visual Basic)
Private (Visual Basic)
Public (Visual Basic)