A declaration specifies the accessibility of the entity it declares. An entity's accessibility does not change the scope of an entity's name. The accessibility domain of a declaration is the set of all declaration spaces in which the declared entity is accessible.
The five access types are Public, Protected, Friend, Protected Friend, and Private. Public is the most permissive access type, and the four other types are all subsets of Public. The least permissive access type is Private, and the four other access types are all supersets of Private.
The access type for a declaration is specified via an optional access modifier, which can be Public, Protected, Friend, Private, or the combination of Protected and Friend. If no access modifier is specified, the default access type depends on the declaration context; the permitted access types also depend on the declaration context.
- Entities declared with the Public modifier have Public access. There are no restrictions on the use of Public entities.
- Entities declared with the Protected modifier have Protected access. Protected access can only be specified on members of classes (both regular type members and nested classes) or on Overridable members of standard modules and structures (which must, by definition, be inherited from System.Object or System.ValueType). A Protected member is accessible to a derived class, provided that either the member is not an instance member, or the access takes place through an instance of the derived class. Protected access is not a superset of Friend access.
- Entities declared with the Friend modifier have Friend access. An entity with Friend access is accessible only within the program that contains the entity declaration.
- Entities declared with the Protected Friend modifiers have the union of Protected and Friend access.
- Entities declared with the Private modifier have Private access. A Private entity is accessible only within its declaration context, including any nested entities.
The accessibility in a declaration does not depend on the accessibility of the declaration context. For example, a type declared with Private access may contain a type member with Public access.
The following code demonstrates various accessibility domains:
Public Class A
Public Shared X As Integer
Friend Shared Y As Integer
Private Shared Z As Integer
End Class
Friend Class B
Public Shared X As Integer
Friend Shared Y As Integer
Private Shared Z As Integer
Public Class C
Public Shared X As Integer
Friend Shared Y As Integer
Private Shared Z As Integer
End Class
Private Class D
Public Shared X As Integer
Friend Shared Y As Integer
Private Shared Z As Integer
End Class
End Class The classes and members have the following accessibility domains:
- The accessibility domain of
A and A.X is unlimited. - The accessibility domain of
A.Y, B, B.X, B.Y, B.C, B.C.X, and B.C.Y is the containing program. - The accessibility domain of
A.Z is A. - The accessibility domain of
B.Z and B.D is B, including B.C and B.D. - The accessibility domain of
B.C.Z is B.C. - The accessibility domain of
B.D.X, B.D.Y, and B.D.Z is B.D.
As the example illustrates, the accessibility domain of a member is never larger than that of a containing type. For example, even though all X members have Public declared accessibility, all but A.X have accessibility domains that are constrained by a containing type.
Access to Protected instance members must be through an instance of the derived type so that unrelated types cannot gain access to each other's protected members. For example:
Public Class User
Protected Password As String
End Class
Public Class Employee
Inherits User
End Class
Public Class Guest
Inherits User
Public Function GetPassword(ByVal U As User) As String
' Error: protected access has to go through derived type.
Return U.Password
End Function
End Class In the above example, the class Guest only has access to the protected Password field if it is qualified with an instance of Guest. This prevents Guest from gaining access to the Password field of an Employee object simply by casting it to User.
AccessModifier ::= Public | Protected | Friend | Private
See Also
4.1 Declarations | 4.3 Inheritance | 4.4 Implementation | 4.5 Polymorphism | 4.2 Scope | 4.7 Type and Namespace Names | 4.7.1 Qualified Name Resolution | 5. Attributes | Accessibility (Visual Basic Language Concepts)