Visual Basic (VB) Access Modifiers (Public, Private, Protected, Friend)

HTML Source EditorWord Wrap

In visual basic, Access Modifiers are the keywords and those are useful to define an accessibility level for all the types and type members.

 

By specifying the access level for all the types and type members, we can control whether they can be accessed in other classes or in current assembly or in other assemblies based on our requirements.

 

The following are the different types of access modifiers available in a visual basic programming language.

 

By using these four access modifiers, we can specify a following six levels of accessibility for all types and type members based on our requirements.

 

Access ModifierDescription
Public It is used to specifies that access is not restricted.
Private It is used to specifies that access is limited to the containing type.
Protected It is used to specifies that the access is limited to the containing type or types derived from the containing class.
Friend (internal) It is used to specifies that access is limited to the current assembly.
Protected Friend It is used to specifies that access is limited to the current assembly or types derived from the containing class.

Generally, in visual basic only one access modifier is allowed to use with any member or type, except when we use Protected Friend combination.

 

In visual basic, we are not allowed to use any access modifiers on namespaces, because the namespaces have no access restrictions.

 

Only certain access modifiers are allowed to specify based on the context in which the member declaration occurs. In case, if we didn’t mention any access modifiers during the member declaration, then the default access modifiers will be used depending on the member declaration context.

 

For example, the top-level types which are not nested in any other types can only have Public or Friend accessibility. The default accessibility for top-level types is Friend.

Visual Basic Public Access Modifier

In visual basic, Public modifier is useful to specify that access is not restricted, so the defined type or member can be accessed by any other code in the current assembly or another assembly that references it.

 

Following is the example of defining members with Public modifier in a visual basic programming language.

 

Module Module1

Class User

Public Name As String

Public Location As String

Public Age As Integer

Public Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Sub Main()

Dim u As User = New User()

u.Name = "Suresh Dasari"

u.Location = "Hyderabad"

u.Age = 32

u.GetUserDetails()

Console.WriteLine(vbLf & "Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Module

If you observe the above example, we defined a User class with required variables and method using Public access modifier and trying to access those variables and method in another class with an object reference of User class.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic (VB) public access modifier example result

 

If you observe the above result, we are able to access the variables and methods of User class in another class because of specifying with Public specifier based on our requirements.

 

As discussed, the Public access specifier will make all the defined members or types available to all the types in our application.

Visual Basic Private Access Modifier

In visual basic, Private modifier is useful to specify that access is limited to the containing type so the defined type or member can only be accessed by the code in the same class or structure.

 

Following is the example of defining members with Private modifier in a visual basic programming language.

 

Module Module1

Class User

Private Name As String

Private Location As String

Private Age As Integer

Private Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Sub Main()

Dim u As User = New User()

' Complie-time Error

' These are inaccessible due to private specifier

u.Name = "Suresh Dasari"

u.Location = "Hyderabad"

u.Age = 32

u.GetUserDetails()

Console.WriteLine(vbLf & "Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Module

If you observe the above example, we defined a User class with required variables and method using Private access modifier and trying to access those variables and method in another class with an object reference of User class.

 

When we execute the above visual basic program, we will get compile-time errors like as shown below.

 

Visual basic (vb) private access modifier example result

 

If you observe the above result, we got the compile-time error because the Private modifier members of User class referred in another class.

 

As discussed, the Private modifier type or member can be accessed only by the code within the same class or structure.

Visual Basic Protected Access Modifier

In visual basic, Protected modifier is useful to specify that access is limited to the containing type or types derived from the containing class so the type or member can only be accessed by code within the same class or in a derived class.

 

Following is the example of defining members with Protected modifier in a visual basic programming language.

 

Module Module1

Class User

Protected Name As String

Protected Location As String

Protected Age As Integer

Protected Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Sub Main()

Dim u As User = New User()

' Complie-time Error

' These are inaccessible due to protected specifier

u.Name = "Suresh Dasari"

u.Location = "Hyderabad"

u.Age = 32

u.GetUserDetails()

Console.WriteLine(vbLf & "Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Module

If you observe the above example, we defined a User class with required variables and method using Protected access modifier and trying to access those variables and method in another class with an object reference of User class.

 

When we execute the above visual basic program, we will get compile-time errors like as shown below.

 

Visual basic (vb) protected access modifier example result

 

If you observe the above result, we got a compile-time error because the Protected modifier members of User class referred in another class.

 

As discussed, the Protected members of a base class can be accessible in a derived class, only when access occurs through the derived class type.

 

Following is the example of accessing a base class Protected members in derived class through derived class type.

 

Class User

Protected Name As String

Protected Location As String

Protected Age As Integer

Protected Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Class Details

Inherits User

Public Shared Sub Main()

Dim u As User = New User()

Dim d As Details = New Details()

' Complier Error

' protected members can only accessible with derived classes

' u.Name = "Suresh Dasari";

d.Name = "Suresh Dasari"

d.Location = "Hyderabad"

d.Age = 32

d.GetUserDetails()

Console.WriteLine(vbLf & "Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Class

If you observe the above example, we are accessing base class (User) Protected members using the reference of derived class (Details) and if we uncomment the commented code we will get a compile-time error because we are trying to access the protected members with base class (User) reference instead of derived class (Details).

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual basic (vb) protected access modifier example result

 

This is how we can use Protected modifier in our visual basic applications to limit access of type or member in the same class or derived class based on our requirements.

 

In visual basic, the structure members cannot be Protected because the structure cannot be inherited.

Visual Basic Friend Access Modifier

In visual basic, Friend modifier is useful to specify that access is limited to the current assembly so the type or member can be accessed by any code in the same assembly, but not from another assembly.

 

Following is the example of defining members with Friend modifier in a visual basic programming language.

 

Module Module1

Class User

Friend Name As String

Friend Location As String

Friend Age As Integer

Friend Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Sub Main()

Dim u As User = New User()

u.Name = "Suresh Dasari"

u.Name = "Suresh Dasari"

u.Location = "Hyderabad"

u.Age = 32

u.GetUserDetails()

Console.WriteLine(vbLf & "Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Module

If you observe the above example, we defined a User class with required variables and method using Friend access modifier and trying to access those variables and method in another class with an object reference of User class.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual basic (vb) friend access modifier example result

 

If you observe the above result, we are able to access the variables and methods of User class in another class because of specifying with Friend specifier based on our requirements.

 

As discussed, in visual basic the Friend type or members are accessible within the files of the same assembly.

Visual Basic Protected Friend Access Modifier

In visual basic, the Protected Friend modifier is useful to specify that access is limited to the current assembly or types derived from the containing class. So, the type or member can be accessed by any code in the same assembly or by any derived class in another assembly.

 

Following is the example of defining members with Protected Friend modifier in a visual basic programming language.

 

Module Module1

Class User

Protected Friend Name As String

Protected Friend Location As String

Protected Friend Age As Integer

Protected Friend Sub GetUserDetails()

Console.WriteLine("Name: {0}", Name)

Console.WriteLine("Location: {0}", Location)

Console.WriteLine("Age: {0}", Age)

End Sub

End Class

Sub Main()

Dim u As User = New User()

u.Name = "Suresh Dasari"

u.Name = "Suresh Dasari"

u.Location = "Hyderabad"

u.Age = 32

u.GetUserDetails()

Console.WriteLine("Press Enter Key to Exit..")

Console.ReadLine()

End Sub

End Module

If you observe the above example, we defined a User class with required variables and method using Protected Friend access modifier and trying to access those variables and method in another class with an object reference of User class.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual basic protected friend access modifier example result

 

If you observe the above result, we are able to access the variables and methods of User class in another class because of specifying with Friend specifier based on our requirements.

 

As discussed, in visual basic the Protected Friend type or members are accessible from the current assembly or from the types that are derived from the containing class in another assembly.