Visual Basic Encapsulation

In visual basic, Encapsulation is a process of binding the data members and member functions into a single unit. In visual basic, class is the real-time example for encapsulation because it will combine various type of data members and member functions into a single unit.

 

Generally, in visual basic the encapsulation is useful to prevent alteration of code (data) accidentally from the outside of functions. In visual basic, by defining the class fields with properties we can protect the data from accidental corruption.

 

If we define class fields with properties, then the encapsulated class won’t allow us to access the fields directly instead, we need to use getter and setter functions to read or write a data based on our requirements.

 

Following is the example of defining an encapsulation class using properties with Get and Set accessors.

 

Class User

    Private location As String

    Private name As String

    Public Property Ulocation() As String

        Get

            Return location

        End Get

        Set(ByVal value As String)

            location = value

        End Set

    End Property

    Public Property Uname() As String

        Get

            Return name

        End Get

        Set(ByVal value As String)

            name = value

        End Set

    End Property

End Class

If you observe the above code, we defined variables with private access modifiers and exposing those variables in a public way by using properties Get and Set accessors. In case, if you want to make any modifications to the defined variables, we can make it by using properties with Get and Set accessors.

Visual Basic Encapsulation Example

Following is the example of defining an encapsulated class in visual basic programming language.

 

Module Module1

    Class User

        Private location As String

        Private name As String

        Public Property Ulocation() As String

            Get

                Return location

            End Get

            Set(ByVal value As String)

                location = value

            End Set

        End Property

        Public Property Uname() As String

            Get

                Return name

            End Get

            Set(ByVal value As String)

                name = value

            End Set

        End Property

    End Class

    Sub Main()

        Dim u As User = New User()

        ' Set accessor will invoke

        u.Uname = "Suresh Dasari"

        ' Set accessor will invoke

        u.Ulocation = "Hyderabad"

        ' Get accessor will invoke

        Console.WriteLine("Name: " & u.Uname)

        ' Get accessor will invoke

        Console.WriteLine("Location: " & u.Ulocation)

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

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined fields in encapsulated class using properties and we are able to manipulate field values using Get and Set accessors of properties.

 

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

 

Visual Basic (VB) Encapsulation Example Result

 

This is how we can use encapsulation in visual basic programming language to bind the data members and member functions into single unit by protecting the data from accidental corruption.

Topics Covered