<%@ Page %>
Understanding Access Modifiers in VB.NET
Introduction
Access modifiers decide accessibility of your class or class member. There are five accessibility levels in VB.NET. They are:
- Private
- Protected
- Friend (internal in C#)
- Protected friend (protected internal in C#)
- Public
This article examines all them with examples. Even though the examples are in VB.NET, they can be easily ported to C# as most of the keywords are same.
Public Access
Many programmers have habit of making everything public in their applications. This is fine for test applications but when you are developing real life applications you should expose only the data or functionality that is necessary by the user of your class. Classes and class members marked with Public access modifier are available in the same class, all the classes from the same project and to all other projects as well. This access specifier is least restrictive. Following is an example of public access specifier.
Public Class Book
Public Title As String
End Class
Public Class BookUser
Public Sub SomeMethod()
Dim x as new Book()
x.Title="VB.NET Programming"
End Sub
End Class
Restricting access to classes and class members is not only a good programming practice but is also necessary to avoid any accidental misuse of your classes.
Let us now discuss each of the remaining access modifiers in detail
Private Access
Private access modifier is applicable only to the members of a type. It restricts access to the members within the type itself. Consider following class:
Public Class Book
Private strTitle As String
Public Property Title()
Get
Return strTitle
End Get
Set(ByVal Value)
strTitle = Value
End Set
End Property
End Class
Here, the member variable strTitle is declared as private inside a class Book. Hence, it cannot be accessed from outside the class Book. Remember that private access modifier is applicable only to type members not to the type itself. This means you cannot declare a class as private but if your class is a nested then it can be declared as private. For example following declaration is invalid:
Namespace n1
Private Class Book
End Class
End Namespace
However, following declaration of nested class is valid:
Namespace n1
Public Class Book
Private Class NestedBook
End Class
End Class
End Namespace
Protected Access
Private access modifier allows us to hide members from others but what if some one is inheriting from your class? In many cases you want that members of base class should be available in derived class. This cannot be achieved with private modifier. Protected access specifier
provide such access. The members marked with protected access modifier can be accessed in the same class and all the classes inherited from it but they are not available to other classes. Following is an example of using protected access specifier:
Public Class Class1
Protected age As Integer
'... other code
End Class
Public Class Class2
Inherits Class1
Public Sub SomeMethod()
age = 99 'OK
End Sub
End Class
Public Class Class3
Public Sub SomeMethod()
Dim x As New Class1()
x.age = 99 'ERROR
End Sub
End Class
Friend Access
Now going one step further let us assume that you want that all the classes from your project should be able to access to your class members but classes external to your project should not be able to do so. In this case neither private nor protected can help. Only your �Friend� can help you out. You guessed it! The friend access modifier is used to declare your class members such that any class from the same project will be able to access them but external classes cannot. Note that this access modifier is applicable to class definitions also. Following are the examples of using Friend members and classes.
Assembly1.dll
Public Class Class1
Friend age As Integer
'... other code
End Class
Public Class Class2
Public Sub SomeMethod()
Dim x As New Class1()
x.age = 99 'OK
End Sub
End Class
Assembly2.dll
Public Class Class3
Public Sub SomeOtherMethod()
Dim x As New Class1()
x.age = 99 'ERROR
End Sub
End Class
When applied to class the class will be available only in the project in which it is declared.
Assembly1.dll
Friend Class Class3
Public Sub SomeMethod()
End Sub
End Class
Public Class Class4
Public Sub SomeOtherMethod()
Dim x As Class3 'OK
End Sub
End Class
Assembly2.dll
Dim x As TestComp1.n1.Class3 'ERROR
Note: In C# internal keyword serves the same purpose as Friend keyword in VB.NET.
Protected Friend Access
Protected Friend access modifier is a combination of protected and friend access modifiers and allows access to class members in the same project and all the inherited types.
Summary
This article examined various access modifiers available in .NET. Restricting access to your class members is not only a good practice but also make your classes less error prone and robust. They will also avoid any accidental misuse of class members.