Write A Program in VB Net To Implement Encapsulation in Class
Write A Program in VB Net To Implement Encapsulation in Class
(/)
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.
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.
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
https://www.tutlane.com/tutorial/visual-basic/vb-encapsulation 3/3