Public (Visual Basic)Public (Visual Basic)

指定一或多個宣告的程式設計項目有沒有存取限制。Specifies that one or more declared programming elements have no access restrictions.


如果您要發行的元件或一組元件,例如類別程式庫,您通常會想要存取您的組件與相互操作的任何程式碼的程式設計項目。If you are publishing a component or set of components, such as a class library, you usually want the programming elements to be accessible by any code that interoperates with your assembly. 若要授與這類項目上的無限制的存取,您可以將它與宣告PublicTo confer such unlimited access on an element, you can declare it with Public.

當您不需要限制其存取權時,公用存取是正常的層級的程式設計項目。Public access is the normal level for a programming element when you do not need to limit access to it. 請注意,元素的存取層級宣告的介面、 模組、 類別或結構中的預設值為Public如果您不要否則宣告它。Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise.


  • 宣告內容。Declaration Context. 您可以使用Public只能在模組、 介面或命名空間層級。You can use Public only at module, interface, or namespace level. 這表示的宣告內容Public項目必須是原始程式檔、 命名空間、 介面、 模組、 類別或結構,而且不能是程序。This means the declaration context for a Public element must be a source file, namespace, interface, module, class, or structure, and cannot be a procedure.


  • 存取層級。Access Level. 模組、 類別或結構可以存取的所有程式碼可以存取其Public項目。All code that can access a module, class, or structure can access its Public elements.

  • 預設存取權。Default Access. 本機變數內程序預設值為公用存取,因此您無法在其上使用任何存取修飾詞。Local variables inside a procedure default to public access, and you cannot use any access modifiers on them.

  • 存取修飾詞。Access Modifiers. 指定存取層級的關鍵字稱為存取修飾詞The keywords that specify access level are called access modifiers. 如需存取修飾詞的比較,請參閱 < 存取 Visual Basic 中的層級For a comparison of the access modifiers, see Access levels in Visual Basic.

Public 修飾詞可用於以下內容:The Public modifier can be used in these contexts:

Class 陳述式Class Statement

Const 陳述式Const Statement

Declare 陳述式Declare Statement

Delegate 陳述式Delegate Statement

Dim 陳述式Dim Statement

Enum 陳述式Enum Statement

Event 陳述式Event Statement

Function 陳述式Function Statement

Interface 陳述式Interface Statement

Module 陳述式Module Statement

Operator 陳述式Operator Statement

Property 陳述式Property Statement

Structure 陳述式Structure Statement

Sub 陳述式Sub Statement

另請參閱See also