Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual Basic
F-O Keywords
 Friend

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Basic Language Reference
Friend (Visual Basic)

Specifies that one or more declared programming elements are accessible only from within the assembly that contains their declaration.

Rules

  • Declaration Context. You can use Friend only at module, interface, or namespace level. This means the declaration context for a Friend element must be a source file, namespace, interface, module, class, or structure, and cannot be a procedure.

  • Combined Modifiers. You can use the Friend modifier in conjunction with the Protected (Visual Basic) modifier in the same declaration. This combination confers both friend and protected access on the declared elements, so they are accessible from anywhere in the same assembly, from their own class, and from derived classes. You can specify Protected Friend only on members of classes.

Behavior

  • Access Level. All code within a declaration context can access its elements. Code in other classes, structures, and modules that are compiled to the same assembly can access all the Friend elements in that assembly.

    Friend access is not a superset or subset of protected access.

  • Access Modifiers. The keywords that specify access level are called access modifiers. For a comparison of the access modifiers, see Access Levels in Visual Basic.

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. If you want to limit access to an element in this way, you can declare it with Friend.

Friend access is often the preferred level for an application's programming elements. Note that the access level of an interface, module, class, or structure defaults to Friend if you do not declare it otherwise.

The Friend modifier can be used in these contexts:

Class Statement

Const Statement

Declare Statement

Delegate Statement

Dim Statement

Enum Statement

Event Statement

Function Statement

Interface Statement

Module Statement

Property Statement

Structure Statement

Sub Statement

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker