| title | Friend (Visual Basic) | |||||
|---|---|---|---|---|---|---|
| ms.date | 07/20/2015 | |||||
| ms.prod | .net | |||||
| ms.suite | ||||||
| ms.technology |
|
|||||
| ms.topic | article | |||||
| f1_keywords |
|
|||||
| helpviewer_keywords |
|
|||||
| ms.assetid | b664605e-1c79-4728-b996-aa59c50846bc | |||||
| caps.latest.revision | 25 | |||||
| author | dotnet-bot | |||||
| ms.author | dotnetcontent |
Specifies that one or more declared programming elements are accessible only from within the assembly that contains their declaration.
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 by using the Friend modifier.
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 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.
You can use Friend only at the module, interface, or namespace level. 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.
You can use the Friend modifier in conjunction with the Protected modifier in the same declaration. This combination confers both Friend access 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.
For a comparison of Friend and the other access modifiers, see Access levels in Visual Basic.
Note
You can specify that another assembly is a friend assembly, which allows it to access all types and members that are marked as Friend. For more information, see Friend Assemblies.
The following class uses the Friend modifier to allow other programming elements within the same assembly to access certain members.
[!code-vbVbVbalrAccessModifiers#1]
You can use the Friend modifier in these contexts:
xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute
Public
Protected
Private
Access levels in Visual Basic
Procedures
Structures
Objects and Classes