宣告內容和預設存取層級 (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 (PublicStructure,不允許在Interface)Private (Public in Structure, not allowed in Interface) Public
常數 (Const 陳述式)Constant (Const Statement) 不允許Not allowed Private (PublicStructure,不允許在Interface)Private (Public in Structure, not allowed in Interface) Public
列舉型別 (Enum 陳述式)Enumeration (Enum Statement) Friend Public 不允許Not allowed
類別 (Class 陳述式)Class (Class Statement) Friend Public 不允許Not allowed
結構 (結構,陳述式)Structure (Structure Statement) Friend Public 不允許Not allowed
模組 (Module 陳述式)Module (Module Statement) Friend 不允許Not allowed 不允許Not allowed
介面 (Interface 陳述式)Interface (Interface Statement) Friend Public 不允許Not allowed
程序 (Function 陳述式Sub 陳述式)Procedure (Function Statement, Sub Statement) 不允許Not allowed Public 不允許Not allowed
外部參考 (Declare 陳述式)External reference (Declare Statement) 不允許Not allowed Public (不允許在Interface)Public (not allowed in Interface) 不允許Not allowed
運算子 (Operator 陳述式)Operator (Operator Statement) 不允許Not allowed Public (在中不允許InterfaceModule)Public (not allowed in Interface or Module) 不允許Not allowed
屬性 (Property Statement)Property (Property Statement) 不允許Not allowed Public 不允許Not allowed
預設屬性 (預設)Default property (Default) 不允許Not allowed Public (不允許在Module)Public (not allowed in Module) 不允許Not allowed
事件 (Event 陳述式)Event (Event Statement) 不允許Not allowed Public 不允許Not allowed
委派 (Delegate 陳述式)Delegate (Delegate Statement) Friend Public 不允許Not allowed

如需詳細資訊,請參閱 < 存取 Visual Basic 中的層級For more information, see Access levels in Visual Basic.

另請參閱See also