Access Modifiers in VB.net with Example – List of Access Modifiers

What is Access Modifiers in VB.net?

Access Modifiers in VB.net 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.

List of Access Modifiers in VB.net

The keywords that specify access level are called access modifiers.

Access Modifiers in VB.netDescription
PublicAny code that can see a public element can access it.
ProtectedCode in the class that declares a protected element, or a class derived from it, can access the element.
FriendCode in the assembly that declares a friend element can access it.
Protected FriendCode 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.
PrivateCode in the type that declares a private element, including code within contained types, can access the element.
Private ProtectedCode in the class that declares a private protected element, or code in a derived class found in the same assembly as the base class.
VB.net Access Modifiers

Public

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 of Access Modifiers in VB.net:

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

  • You can use Public only at module, interface, or namespace level.
  • You can declare a public element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.

Protected

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 the required variables and methods using the Protected access modifier.

  • You can use Protected only at class level, and only when you declare a member of a class.
  • You can declare a protected element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.

Friend

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
  • You can use Friend only at the module, interface, or namespace level.
  • You can declare a friend element at the level of a source file or namespace, or inside an interface, module, class, or structure, but not in a procedure.

Protected Friend

The Protected Friend keyword combination in the declaration statement specifies that the element can be accessed either from derived classes 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
  • You can use Protected Friend only at class level, and only when you declare a member of a class.
  • You can declare a protected friend element in a class, but not at the level of a source file or namespace, or inside an interface, module, structure, or procedure.

Private

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.

  • At the module level, the Dim statement without any access level keywords is equivalent to a Private declaration.
  • However, you might want to use the Private keyword to make your code easier to read and interpret.

Private Protected

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

  • You can declare a Private Protected element only inside of a class.
  • You cannot declare it within an interface or structure, nor can you declare it at the level of a source file or namespace, inside an interface or a structure, or in a procedure.

List of Available Modifiers in VB.net

The following table provides the complete list of VB.net modifiers.

Sr.NoModifiers in VB.netDescription
1.AnsiSpecifies that Visual Basic should marshal all strings to American National Standards Institute (ANSI) values regardless of the name of the external procedure being declared.
2.AssemblySpecifies that an attribute at the beginning of a source file applies to the entire assembly.
3.AsyncIndicates that the method or lambda expression that it modifies is asynchronous. Such methods are referred to as async methods. The caller of an async method can resume its work without waiting for the async method to finish.
4.AutoThe charsetmodifier part in the Declare statement supplies the character set information for marshaling strings during a call to the external procedure. It also affects how Visual Basic searches the external file for the external procedure name. The Auto modifier specifies that Visual Basic should marshal strings according to .NET Framework rules.
5.ByRefSpecifies that an argument is passed by reference, i.e., the called procedure can change the value of a variable underlying the argument in the calling code. It is used under the contexts of :

Declare Statement
– Function Statement
– Sub Statement
6.ByValSpecifies that an argument is passed in such a way that the called procedure or property cannot change the value of a variable underlying the argument in the calling code. It is used under the contexts of:

– Declare Statement
– Function Statement
– Operator Statement
– Property Statement
– Sub Statement
7.DefaultIdentifies a property as the default property of its class, structure, or interface.
8.FriendSpecifies that one or more declared programming elements are accessible from within the assembly that contains their declaration, not only by the component that declares them.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.
9.InIt is used in generic interfaces and delegates.
10.IteratorSpecifies that a function or Get accessor is an iterator. An iterator performs a custom iteration over a collection.
11.KeyThe Key keyword enables you to specify behavior for properties of anonymous types.
12.ModuleSpecifies that an attribute at the beginning of a source file applies to the current assembly module. It is not same as the Module statement.
13.MustInheritSpecifies that a class can be used only as a base class and that you cannot create an object directly from it.
14.MustOverrideSpecifies that a property or procedure is not implemented in this class and must be overridden in a derived class before it can be used.
15.NarrowingIndicates that a conversion operator (CType) converts a class or structure to a type that might not be able to hold some of the possible values of the original class or structure.
16.NotInheritableSpecifies that a class cannot be used as a base class.
17.NotOverridableSpecifies that a property or procedure cannot be overridden in a derived class.
18.OptionalSpecifies that a procedure argument can be omitted when the procedure is called.
19.OutFor generic type parameters, the Out keyword specifies that the type is covariant.
20.OverloadsSpecifies that a property or procedure redeclares one or more existing properties or procedures with the same name.
21.OverridableSpecifies that a property or procedure can be overridden by an identically named property or procedure in a derived class.
22.OverridesSpecifies that a property or procedure overrides an identically named property or procedure inherited from a base class.
23.ParamArrayParamArray allows you to pass an arbitrary number of arguments to the procedure. A ParamArray parameter is always declared using ByVal.
24.PartialIndicates that a class or structure declaration is a partial definition of the class or structure.
25.PrivateSpecifies that one or more declared programming elements are accessible only from within their declaration context, including from within any contained types.
26.ProtectedSpecifies that one or more declared programming elements are accessible only from within their own class or from a derived class.
27.PublicSpecifies that one or more declared programming elements have no access restrictions.
28.ReadOnlySpecifies that a variable or property can be read but not written.
29.ShadowsSpecifies that a declared programming element redeclares and hides an identically named element, or set of overloaded elements, in a base class.
30.SharedSpecifies that one or more declared programming elements are associated with a class or structure at large, and not with a specific instance of the class or structure.
31.StaticSpecifies that one or more declared local variables are to continue to exist and retain their latest values after termination of the procedure in which they are declared.
32.UnicodeSpecifies that Visual Basic should marshal all strings to Unicode values regardless of the name of the external procedure being declared.
33.WideningIndicates that a conversion operator (CType) converts a class or structure to a type that can hold all possible values of the original class or structure.
34.WithEventsSpecifies that one or more declared member variables refer to an instance of a class that can raise events.
35.WriteOnlySpecifies that a property can be written but not read.
List of Available Modifiers in VB.net

Summary

One of the most confusing aspects of OOP is determining the proper access modifier to use for classes and class data members and methods.

If you provide too much scope for a data member or method, you risk violating data encapsulation, leading to unwanted data access.

If your scope is too restrictive, other classes that need access to a class’s data or methods won’t be able to access them.

In this chapter we discuss the different access modifiers, how they are used to manage class members and method scoping, and when to use each modifier.

At the end of the chapter, we discuss how these access modifiers work with the classes themselves.


Leave a Comment