Private (Visual Basic)Private (Visual Basic)

指定一或多個宣告的程式設計元素,只能從其宣告的內容,包括從任何所包含的型別中存取。Specifies that one or more declared programming elements are accessible only from within their declaration context, including from within any contained types.

備註Remarks

如果程式設計項目代表專屬的功能,或包含機密資料,您通常會想要盡可能地限制其存取權。If a programming element represents proprietary functionality, or contains confidential data, you usually want to limit access to it as strictly as possible. 您藉由只模組、 類別或結構,定義以存取它達到最大的限制。You achieve the maximum limitation by allowing only the module, class, or structure that defines it to access it. 若要限制存取的項目,如此一來,您可以將它與宣告PrivateTo limit access to an element in this way, you can declare it with Private.

注意

您也可以使用Private Protected存取修飾詞,讓成員存取從該類別中,並從其包含的組件中的衍生類別。You can also use the Private Protected access modifier, which makes a member accessible from within that class and from derived classes located in its containing assembly.

規則Rules

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

行為Behavior

  • 存取層級。Access Level. 宣告內容中的所有程式碼可以存取其Private項目。All code within a declaration context can access its Private elements. 這包括程式碼中包含的型別,例如巢狀的類別或列舉型別中的,指派運算式。This includes code within a contained type, such as a nested class or an assignment expression in an enumeration. 宣告內容之外的任何程式碼可以存取其Private項目。No code outside of the declaration context can access its Private elements.

  • 存取修飾詞。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.

Private 修飾詞可用於以下內容:The Private 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

Property 陳述式Property Statement

Structure 陳述式Structure Statement

Sub 陳述式Sub Statement

另請參閱See also