In visual basic, access modifiers are the keywords, and those are useful to define an accessibility level for all the types and type members. Access modifiers can control whether they can be accessed in other classes or current assembly or other assemblies based on your requirements.
The keywords that specify access level are called access modifiers.
Access Modifier | Description |
---|---|
Public | Any code that can see a public element can access it. |
Protected | Code in the class that declares a protected element, or a class derived from it, can access the element. |
Friend | Code in the assembly that declares a friend element can access it. |
Protected Friend | Code in the same class or the same assembly as a protected friend element, or within any class derived from the element's class, can access it. |
Private | Code in the type that declares a private element, including code within contained types, can access the element. |
Private Protected | Code in the class that declares a private protected element, or code in a derived class found in the same assembly as the base class. |
The Public
keyword in the declaration statement specifies that the element can be accessed from code anywhere in the same project, from other projects that reference the project, and from any assembly built from the project.
The following code shows a simple Public declaration:
Public Class Customer
Public Property Id As Integer
Public Property Name As String
Public Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Public Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
Public
only at module, interface, or namespace level.The Protected
keyword in the declaration statement specifies that the element can be accessed only from within the same class, or from a class derived from this class.
The following code shows a simple Protected declaration:
Public Class Customer
Protected Property Id As Integer
Protected Property Name As String
Protected Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Protected Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
In the above example, you can see that a Customer
class is defined with required variables and method using the Protected
access modifier
Protected
only at class level, and only when you declare a member of a class.The Friend
keyword in the declaration statement specifies that the element can be accessed from within the same assembly, but not from outside the assembly.
The following code shows a simple Friend
declaration.
Public Class Customer
Friend Property Id As Integer
Friend Property Name As String
Friend Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Friend Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
Friend
only at the module, interface, or namespace level.The Protected Friend
keyword combination in the declaration statement specifies that the element can be accessed either from derived classes or from within the same assembly, or both.
The following code shows a simple Protected Friend
declaration.
Public Class Customer
Protected Friend Property Id As Integer
Protected Friend Property Name As String
Protected Friend Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Protected Friend Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
Protected Friend
only at class level, and only when you declare a member of a class.The Private
keyword in the declaration statement specifies that the element can be accessed only from within the same module, class, or structure.
The following code shows a simple Private
declaration.
Public Class Customer
Private Property Id As Integer
Private Property Name As String
Private Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Public Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
You can use Private
only at the module level. IT means you can declare a private element inside a module, class, or structure, but not at the level of a source file or namespace, inside an interface, or in a procedure.
Dim
statement without any access level keywords is equivalent to a Private
declaration.Private
keyword to make your code easier to read and interpret.The Private Protected
keyword combination in the declaration statement specifies that the element can be accessed only from within the same class, as well as from derived classes found in the same assembly as the containing class.
The Private Protected
access modifier is supported starting with Visual Basic 15.5. The following example shows a Private Protected
declaration.
Public Class Customer
Private Protected Property Id As Integer
Private Protected Property Name As String
Private Protected Property Address As String
Public Sub New()
End Sub
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal address As String)
Me.Id = id
Me.Name = name
Me.Address = address
End Sub
Public Sub Print()
Console.WriteLine("Id: {0}, Name: {1}, Address: {2}", Me.Id, Me.Name, Me.Address)
End Sub
End Class
Private Protected
element only inside of a class.