Friend (Visual Basic)Friend (Visual Basic)

指定一或多個宣告的程式設計項目,只能從包含其宣告的元件中存取。Specifies that one or more declared programming elements are accessible only from within the assembly that contains their declaration.

備註Remarks

在許多情況下,您會想要讓整個元件使用程式設計專案(例如類別和結構),而不只是宣告它們的元件。In many cases, you want programming elements such as classes and structures to be used by the entire assembly, not only by the component that declares them. 不過,您可能不想讓程式碼能夠透過元件外部的程式碼存取(例如,如果應用程式是專屬的)。However, you might not want them to be accessible by code outside the assembly (for example, if the application is proprietary). 如果您想要以這種方式限制對專案的存取,您可以使用Friend修飾詞來宣告該元素。If you want to limit access to an element in this way, you can declare it by using the Friend modifier.

其他類別、結構和模組中編譯成相同元件的程式碼,都可以存取該元件Friend中的所有元素。Code in other classes, structures, and modules that are compiled to the same assembly can access all the Friend elements in that assembly.

Friend存取通常是應用程式設計項目的慣用層級,而且Friend是介面、模組、類別或結構的預設存取層級。Friend access is often the preferred level for an application's programming elements, and Friend is the default access level of an interface, a module, a class, or a structure.

您只能在Friend模組、介面或命名空間層級上使用。You can use Friend only at the module, interface, or namespace level. 因此,專案的宣告內容Friend必須是原始程式檔、命名空間、介面、模組、類別或結構,而不能是程式。Therefore, the declaration context for a Friend element must be a source file, a namespace, an interface, a module, a class, or a structure; it can't be a procedure.

注意

您也可以使用受保護的 Friend存取修飾詞,讓類別成員可從類別內、衍生類別,以及定義類別的相同元件中存取。You can also use the Protected Friend access modifier, which makes a class member accessible from within that class, from derived classes, and from the same assembly in which the class is defined. 若要從相同元件中的類別和衍生類別限制成員的存取權,您可以使用私用保護的存取修飾詞。To restrict access to a member from within its class and from derived classes in the same assembly, you use the Private Protected access modifier.

如需Friend和其他存取修飾詞的比較,請參閱Visual Basic 中的存取層級For a comparison of Friend and the other access modifiers, see Access levels in Visual Basic.

注意

您可以指定另一個元件是 friend 元件,讓它能夠存取所有標記為Friend的類型和成員。You can specify that another assembly is a friend assembly, which allows it to access all types and members that are marked as Friend. 如需詳細資訊,請參閱 Friend Assemblies (Friend 組件)。For more information, see Friend Assemblies.

範例Example

下列類別會使用Friend修飾詞,允許相同元件中的其他程式設計項目存取特定成員。The following class uses the Friend modifier to allow other programming elements within the same assembly to access certain members.

Class CustomerInfo

    Private p_CustomerID As Integer

    Public ReadOnly Property CustomerID() As Integer
        Get
            Return p_CustomerID
        End Get
    End Property

    ' Allow friend access to the empty constructor.
    Friend Sub New()

    End Sub

    ' Require that a customer identifier be specified for the public constructor.
    Public Sub New(ByVal customerID As Integer)
        p_CustomerID = customerID
    End Sub

    ' Allow friend programming elements to set the customer identifier.
    Friend Sub SetCustomerID(ByVal customerID As Integer)
        p_CustomerID = customerID
    End Sub
End Class

使用量Usage

您可以在這些Friend內容中使用修飾詞:You can use the Friend modifier 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

Property 陳述式Property Statement

Structure 陳述式Structure Statement

Sub 陳述式Sub Statement

另請參閱See also