Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Write A Program in VB Net To Implement Encapsulation in Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Write A Program in VB Net To Implement Encapsulation in Class

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4/21/24, 6:23 PM Visual Basic Encapsulation - Tutlane

(/)

Visual Basic Encapsulation


In visual basic, Encapsulation is a process of binding the data members (/tutorial/visual-basic/vb-variables)
and member functions (/tutorial/visual-basic/vb-methods-functions) into a single unit. In visual basic, the class
(/tutorial/visual-basic/vb-classes-and-objects) is the real-time example for encapsulation because it will
combine the various types of data members (/tutorial/visual-basic/vb-variables) and member functions
(/tutorial/visual-basic/vb-methods-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 (/tutorial/visual-basic/vb-methods-functions). In visual basic, by defining the class fields
with properties (/tutorial/visual-basic/vb-properties-get-set) we can protect the data from accidental
corruption.

If we define class fields with properties (/tutorial/visual-basic/vb-properties-get-set), then the


encapsulated class (/tutorial/visual-basic/vb-classes-and-objects) won’t allow us to access the fields directly
instead, we need to use getter and setter functions (/tutorial/csharp/csharp-methods-functions-with-
examples) to read or write data based on our requirements.

Following is the example of defining an encapsulation class (/tutorial/visual-basic/vb-classes-and-


objects) using properties (/tutorial/visual-basic/vb-properties-get-set) 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

https://www.tutlane.com/tutorial/visual-basic/vb-encapsulation 1/3
4/21/24, 6:23 PM Visual Basic Encapsulation - Tutlane

If you observe the above code, we defined variables (/tutorial/visual-basic/vb-variables) with private access
modifiers (/tutorial/visual-basic/vb-access-modifiers#divbpvtm) and exposing those variables in a public way
by using properties (/tutorial/visual-basic/vb-properties-get-set) Get and Set accessors. In case, if you want
to make any modifications to the defined variables, we can make it by using properties (/tutorial/visual-
basic/vb-properties-get-set) with Get and Set accessors.

Visual Basic Encapsulation Example


Following is the example of defining an encapsulated class in a 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

https://www.tutlane.com/tutorial/visual-basic/vb-encapsulation 2/3
4/21/24, 6:23 PM Visual Basic Encapsulation - Tutlane

If you observe the above example, we defined fields (/tutorial/visual-basic/vb-variables) in encapsulated class
(/tutorial/visual-basic/vb-classes-and-objects) using properties (/tutorial/visual-basic/vb-properties-get-
set) and we are able to manipulate field (/tutorial/visual-basic/vb-variables) values using Get and Set
accessors of properties (/tutorial/visual-basic/vb-properties-get-set).

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

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

CONTACT US

 Address: No.1-93, Pochamma Colony, Manikonda, Hyderabad, Telangana - 500089

 Email: support@tutlane.com (mailto:support@tutlane.com)

https://www.tutlane.com/tutorial/visual-basic/vb-encapsulation 3/3

You might also like