KEMBAR78
Programming Primer EncapsulationVB | PPTX
1
A Programme Under the compumitra Series
Programming Primer:
Encapsulation and Abstraction
LAB WORK GUIDE
2
OUTLINE
 Encapsulation and Abstraction Using
VB in asp.net
Creating Example Event Handler.
Example Output an Explanation.
Home Exercise.
Summary Review.
3
Encapsulation and Abstraction
Using VB in asp.net
4
EncapsulationVB – Creating Button and Label Template
Button with 'Text' Property set
to 'Encapsulation'.
Two Labels to hold the place
for output display with 'text'
property set to NULL (blank).
• Follow Standard Website Creation Steps and set your path to
C:Learner<student-id>ProgrammingPrimerEncapsulationVB
• Now Create the Execution Event Handler as a button and output
place holders as follows.
5
EncapsulationVB –Copy and Paste Code
Go to 'Default.aspx.cs' and 'Paste' the Code in
'Button1_Click' handler.
Copy this Code
Dim emp As New Employee
emp.EmployeeID = 100
emp.Salary = 8500.35
Label1.Text = "Employee ID = " & emp.EmployeeID.ToString()
Label2.Text = "Employee Salary = " & emp.Salary.ToString()
Dim emp As New Employee
emp.EmployeeID = 100
emp.Salary = 8500.35
Label1.Text="Employee ID = " &
emp.EmployeeID.ToString()
Label2.Text="Employee Salary = " &
emp.Salary.ToString()
6
EncapsulationVB –Copy Code
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class Copy this Code
7
EncapsulationVB –Paste Code
Run Code By
pressing 'F5'
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
Label1.Text = "Employee ID = " & emp.EmployeeID.ToString()
Label2.Text = "Employee Salary = " & emp.Salary.ToString()
Paste code after the End
of '_Default' class
8
Employee emp = new Employee();
emp.EmployeeID = 100;
emp.Salary = 8500.30;
Label1.Text = emp.EmployeeID.ToString();
Label2.Text = emp.Salary.ToString();
EncapsulationVB –Output
Output after executing the
handler using the button.
This 'output' is generated, because we
are trying to fill values in public
properties that are accessible outside
class even though the values are
actually filled in private values such
as ' _salary ' '_employeeID'.
We are able to change this as
this is defined as 'public' in
original class.
Don't worry we shall soon learn 'private' declaration too.
9
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
EncapsulationVB – Example Explanation
This has been defined as 'Private'. This
means that only code written inside
class Employee can modify it.
This is 'Public' as we would like to use a
way to change this property from an
object based on class Employee.
'Public' and 'Private' declarations are known
as access modifiers and they provide the
primary technique of ENCAPSULATION.
Are there other such modifiers? Surely Yes.
10
Class Employee
Private _employeeID As Integer
Private _salary As Double
Public Property EmployeeID() As Integer
Set(ByVal value As Integer)
_employeeID = value
End Set
Get
Return _employeeID
End Get
End Property
Public Property Salary() As Double
Set(ByVal value As Double)
_salary = value
End Set
Get
Return _salary
End Get
End Property
End Class
EncapsulationVB – Example Explanation for ABSTRACTION
Lets Focus on this class 'Employee'.
Suppose I have to define an additional
property 'employ_name' then it is most
likely that I shall define it within this
class.
Means to external world 'Employee' is
an abstract representation of all
behaviours(methods) and
states(properties) of employee and it is
supposed to encapsulate everything
related to this class.
So! Encapsulation helps to attain
ABSTRACTION. Abstraction also
means that class is just a template or a
map. Actual embodiment or physical
usage is where we create an object
based on a class.
11
Dim emp As New Employee
emp ._employeeID = 100
emp ._salary = 8500.3
Label1.Text = "Employee ID = " & emp ._employeeID.ToString()
Label2.Text = "Employee Salary = " & emp._salary.ToString()
Dim emp As New Employee
emp ._employeeID = 100
emp ._salary = 8500.3
Label1.Text = "Employee ID = " & emp ._employeeID.ToString()
Label2.Text = "Employee Salary = " & emp._salary.ToString()
EncapsulationCS – Error Trial
Make necessary changes here and 'Run' code.
Here we shall try to use the same
code with a change to try using
'Private' variable _employeeID
and _salary.
12
EncapsulationVB–Output after changing code
This program will generate a 'Compiler
Error Message: BC30390:
'Employee._employeeID' is not accessible as
it is 'Private'.
Here we see that variable '_employeeID' and '_salary' is actually hidden
behind the class ' Employee'. We can not initialize out side the class by using
object 'emp' of class 'Employee'.
This Error Output Actually proves that the concept of
'ENCAPSULATION' works. Code in Error but we are still very happy.
13
EncapsulationVB : Home Exercise
 You are required to make a program where you can declare a
class 'Mobile'.
 Make a public method sendsms( ).
 Make a protected method typetext( ).
 Call typetext( ) within sendsms( ).
 Now write some eventhandler to show functionality if protected could
be called outside class or not.
 Now further create a class derived from class 'mobile' and name it as
'iphone'.
 Try to use the typetext( ) method now.
We have told you about 'Private' and 'Public' access modifiers. Another common
access modifier used is 'Protected'
A 'Protected' access modifier allows access within a derived (child) class, but not
outside class.
Are there other access modifiers. Try to search internet to find-out and understand.
Difficult?. 'Facing Difficulties is the prime goal of a programmer'. Play some music
and relax. Next is just a summary slide.
14
EncapsulationVB : Learning Summary Review
 Concept of Encapsulation
 Encapsulation helps to provide controlled access to outside classes.
 Encapsulation also helps to bundle similar things together.
 Concept of Abstraction
 Encapsulation helps to attain Abstraction.
 Abstraction also relates to non-physical nature of classes.
 Trying to use a 'Private' data outside the class will give an
error. For this purpose 'Public' declaration should be done.
 A 'Protected' modifier is suitable for neat definition of access
restriction for access to derived classes only.
 Private properties or methods also serve an additional
purpose. If a property or method is private in a class means
the property or methods of same name can be freely
declared in another class.
15
 Ask and guide me at
sunmitraeducation@gmail.com
 Share this information with as
many people as possible.
 Keep visiting www.sunmitra.com
for programme updates.

Programming Primer EncapsulationVB

  • 1.
    1 A Programme Underthe compumitra Series Programming Primer: Encapsulation and Abstraction LAB WORK GUIDE
  • 2.
    2 OUTLINE  Encapsulation andAbstraction Using VB in asp.net Creating Example Event Handler. Example Output an Explanation. Home Exercise. Summary Review.
  • 3.
  • 4.
    4 EncapsulationVB – CreatingButton and Label Template Button with 'Text' Property set to 'Encapsulation'. Two Labels to hold the place for output display with 'text' property set to NULL (blank). • Follow Standard Website Creation Steps and set your path to C:Learner<student-id>ProgrammingPrimerEncapsulationVB • Now Create the Execution Event Handler as a button and output place holders as follows.
  • 5.
    5 EncapsulationVB –Copy andPaste Code Go to 'Default.aspx.cs' and 'Paste' the Code in 'Button1_Click' handler. Copy this Code Dim emp As New Employee emp.EmployeeID = 100 emp.Salary = 8500.35 Label1.Text = "Employee ID = " & emp.EmployeeID.ToString() Label2.Text = "Employee Salary = " & emp.Salary.ToString() Dim emp As New Employee emp.EmployeeID = 100 emp.Salary = 8500.35 Label1.Text="Employee ID = " & emp.EmployeeID.ToString() Label2.Text="Employee Salary = " & emp.Salary.ToString()
  • 6.
    6 EncapsulationVB –Copy Code ClassEmployee Private _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class Copy this Code
  • 7.
    7 EncapsulationVB –Paste Code RunCode By pressing 'F5' Class Employee Private _employeeID As Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class Label1.Text = "Employee ID = " & emp.EmployeeID.ToString() Label2.Text = "Employee Salary = " & emp.Salary.ToString() Paste code after the End of '_Default' class
  • 8.
    8 Employee emp =new Employee(); emp.EmployeeID = 100; emp.Salary = 8500.30; Label1.Text = emp.EmployeeID.ToString(); Label2.Text = emp.Salary.ToString(); EncapsulationVB –Output Output after executing the handler using the button. This 'output' is generated, because we are trying to fill values in public properties that are accessible outside class even though the values are actually filled in private values such as ' _salary ' '_employeeID'. We are able to change this as this is defined as 'public' in original class. Don't worry we shall soon learn 'private' declaration too.
  • 9.
    9 Class Employee Private _employeeIDAs Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class EncapsulationVB – Example Explanation This has been defined as 'Private'. This means that only code written inside class Employee can modify it. This is 'Public' as we would like to use a way to change this property from an object based on class Employee. 'Public' and 'Private' declarations are known as access modifiers and they provide the primary technique of ENCAPSULATION. Are there other such modifiers? Surely Yes.
  • 10.
    10 Class Employee Private _employeeIDAs Integer Private _salary As Double Public Property EmployeeID() As Integer Set(ByVal value As Integer) _employeeID = value End Set Get Return _employeeID End Get End Property Public Property Salary() As Double Set(ByVal value As Double) _salary = value End Set Get Return _salary End Get End Property End Class EncapsulationVB – Example Explanation for ABSTRACTION Lets Focus on this class 'Employee'. Suppose I have to define an additional property 'employ_name' then it is most likely that I shall define it within this class. Means to external world 'Employee' is an abstract representation of all behaviours(methods) and states(properties) of employee and it is supposed to encapsulate everything related to this class. So! Encapsulation helps to attain ABSTRACTION. Abstraction also means that class is just a template or a map. Actual embodiment or physical usage is where we create an object based on a class.
  • 11.
    11 Dim emp AsNew Employee emp ._employeeID = 100 emp ._salary = 8500.3 Label1.Text = "Employee ID = " & emp ._employeeID.ToString() Label2.Text = "Employee Salary = " & emp._salary.ToString() Dim emp As New Employee emp ._employeeID = 100 emp ._salary = 8500.3 Label1.Text = "Employee ID = " & emp ._employeeID.ToString() Label2.Text = "Employee Salary = " & emp._salary.ToString() EncapsulationCS – Error Trial Make necessary changes here and 'Run' code. Here we shall try to use the same code with a change to try using 'Private' variable _employeeID and _salary.
  • 12.
    12 EncapsulationVB–Output after changingcode This program will generate a 'Compiler Error Message: BC30390: 'Employee._employeeID' is not accessible as it is 'Private'. Here we see that variable '_employeeID' and '_salary' is actually hidden behind the class ' Employee'. We can not initialize out side the class by using object 'emp' of class 'Employee'. This Error Output Actually proves that the concept of 'ENCAPSULATION' works. Code in Error but we are still very happy.
  • 13.
    13 EncapsulationVB : HomeExercise  You are required to make a program where you can declare a class 'Mobile'.  Make a public method sendsms( ).  Make a protected method typetext( ).  Call typetext( ) within sendsms( ).  Now write some eventhandler to show functionality if protected could be called outside class or not.  Now further create a class derived from class 'mobile' and name it as 'iphone'.  Try to use the typetext( ) method now. We have told you about 'Private' and 'Public' access modifiers. Another common access modifier used is 'Protected' A 'Protected' access modifier allows access within a derived (child) class, but not outside class. Are there other access modifiers. Try to search internet to find-out and understand. Difficult?. 'Facing Difficulties is the prime goal of a programmer'. Play some music and relax. Next is just a summary slide.
  • 14.
    14 EncapsulationVB : LearningSummary Review  Concept of Encapsulation  Encapsulation helps to provide controlled access to outside classes.  Encapsulation also helps to bundle similar things together.  Concept of Abstraction  Encapsulation helps to attain Abstraction.  Abstraction also relates to non-physical nature of classes.  Trying to use a 'Private' data outside the class will give an error. For this purpose 'Public' declaration should be done.  A 'Protected' modifier is suitable for neat definition of access restriction for access to derived classes only.  Private properties or methods also serve an additional purpose. If a property or method is private in a class means the property or methods of same name can be freely declared in another class.
  • 15.
    15  Ask andguide me at sunmitraeducation@gmail.com  Share this information with as many people as possible.  Keep visiting www.sunmitra.com for programme updates.