0% found this document useful (0 votes)
736 views20 pages

Object-Oriented Programming

The document provides an overview of object-oriented programming concepts in Visual Basic, including encapsulation, classes and objects, class components, and properties. It discusses encapsulation as hiding data and methods within classes. Classes are templates that specify data, methods, and events for objects. Properties provide access to private class data through get and set methods. The document provides examples of creating classes with data members, constructors, read-write, read-only, and write-only properties.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
736 views20 pages

Object-Oriented Programming

The document provides an overview of object-oriented programming concepts in Visual Basic, including encapsulation, classes and objects, class components, and properties. It discusses encapsulation as hiding data and methods within classes. Classes are templates that specify data, methods, and events for objects. Properties provide access to private class data through get and set methods. The document provides examples of creating classes with data members, constructors, read-write, read-only, and write-only properties.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Advance VB Programming

Lecturer : Heng Bora Page - 1 -







OOP
Object-Oriented Programming



Advance VB Programming

Lecturer : Heng Bora Page - 2 -

I. Introduction OOP

Object Oriented Programming (OOP)


Programming. H class, object
H j Encapsulation, Inheritance, Polymorphism

II - Encapsulation
Encapsulation: j H Data
Method Data H Hidden Access Data
Access Method jH
D


t

III- Class and Object
- Class is plan or template that specifies what data, method, and event will reside
in object.
- Object is the instance of the class

1- Create Class

Syntax Create Class


Public Class ClassName
- Data
- Constructor
- Properties
- Methods,
- Events
.
End Class


y Data : the variable, data structure or object that hold the information or data we
want to manipulate.
y Properties : Specialized Procedure that give you access to the private variable in
the class.
y Method : the function or Procedure that makes the object to do thing or take
action.
y Events : Declaration of the action when taken upon the object

Advance VB Programming

Lecturer : Heng Bora Page - 3 -


Example: Create class Customer as below

Public Class Customer
'Data
Private id As Integer
Private name As String
Private sex As String

'Constructor
'Properties
'Methods
'Events
..
End Class

2- Create Object
Syntax:


Dim ObjName As New ClassName



Example: Create Object Customer as below

Dim objCustomer As New Customer

IV- Class Components
1- Accessibility of Class Components
y Private : All variables , properties and methods can use only inside class.
y Public : All variables , properties and methods can use inside class, outside
class and outside Project.
y Protected : All variables , properties and methods can use inside class and
outside class that is inherited.
y Friend: All variables , properties and methods can use inside class, outside
class.

2- General rules for Assigning
y Private Data Variables
y Public Properties
y Public Event
y Public Method
y Protected properties and method

Advance VB Programming

Lecturer : Heng Bora Page - 4 -

3- Constructor
The constructor is a special method that is always invoked as an object is created
y Its name Public Sub New(ArgList)
y Automatically execute before any other methods
y Only runs one for an object
y Do not return value
y Can contain parameters
y You Can add any code that you wish to execute upon creation of the object

Note: Constructor no parameter is default contractor.

Example: Create Class Customer as below

Public Class Customer
'Data
Private id As Integer
Private name As String
Private gender As String
'Constructor
Public Sub New()
id = 1
name = "Jack"
gender = "Male"
End Sub
Public Sub New(ByVal new_id As Integer,
ByValnew_name As String,
ByVal new_gender As String)
id = new_id
name = new_name
gender = new_gender
End Sub
'Properties
'Methods
'Events
End Class


- Create Object for Testing as below

Dim ObjCust1 As New Customer()

Dim ObjCust2 As New Customer(2,"Dara","Male")



Advance VB Programming

Lecturer : Heng Bora Page - 5 -

4- Properties
Properties is specialized procedure that give you access to the private
variable in the class.
Read-Write Properties
Read-Only Properties
Write-Only Properties
Property with Arguments
Default Properties
Object Graph (or Property that return Object)
Enumerated Properties
..

4.1- Read-Write Properties
Syntax:


Public Property PropertyName() As ReturnType
Get
Return value
End Get
Set(Byval value as ReturnType)
Assign Value
End Set
End Property



Example: Create Class Customer as below

Public Class Customer
'Data

Private id As Integer
Private name As String
Private gender As String

'Properties

Public Property PID() As Integer
Get
Return id
End Get
Set(ByVal value As Integer)
id = value
Advance VB Programming

Lecturer : Heng Bora Page - 6 -

End Set
End Property

Public Property PName() As String
Get
Return name
End Get
Set(ByVal value As String)
name = value
End Set
End Property

Public Property PGender() As String
Get
Return gender
End Get
Set(ByVal value As String)
gender = value
End Set
End Property
End Class

- Create Object for Testing as below

Dim ObjCust As New Customer
ObjCust.PID = 3
ObjCust.PName = "Meakara"
ObjCust.PGender = "Female"

MsgBox("ID=" & ObjCust.PID & vbTab & _
"Name=" & ObjCust.PName & vbTab & _
"Gender" & ObjCust.PGender & vbTab)


- The Result










Advance VB Programming

Lecturer : Heng Bora Page - 7 -



4.2- Read-Only Properties

Syntax



Public ReadOnly Property PropertyName() As ReturnType
Get
Return Value
End Get
End Property


Example: Create Class Customer as below

Public Class Customer
'Data
Private id As Integer = 4
Private name As String = "Sopheak"
Private gender As String = "Female"

'Properties
Public ReadOnly Property getID() As Integer
Get
Return id
End Get
End Property

Public ReadOnly Property getName() As String
Get
Return name
End Get
End Property

Public ReadOnly Property getGender() As String
Get
Return gender
End Get
End Property
End Class



Advance VB Programming

Lecturer : Heng Bora Page - 8 -

- Create Object for Testing as below

Dim ObjCust As New Customer
MsgBox("ID=" & ObjCust.getID & vbTab & _
"Name=" & ObjCust.getName & vbTab & _
"Gender" & ObjCust.PGender & vbTab)
- The Result



IV.3.3- Write-Only Properties

Syntax


Public WriteOnly Property PropertyName() as ReturnType
Set(Byval value as type)
Assign Value
End Set
End Property


Example: Create Class Customer as below

Public Class Customer
'Data
Private id As Integer
Private name As String
Private gender As String
'Properties

Public WriteOnly Property setID() As Integer
Set(ByVal value As Integer)
id = value
End Set
End Property

Public WriteOnly Property setName() As String
Advance VB Programming

Lecturer : Heng Bora Page - 9 -

Set(ByVal value As String)
name = value
End Set
End Property

Public WriteOnly Property setGender() As
String
Set(ByVal value As String)
gender = value
End Set
End Property
End Class

- Create Object for Testing as below
Dim ObjCust As New Customer
ObjCust.setID = 5
ObjCust.setName = "Liza"
ObjCust.setGender = "Female"

IV.3.4- Property with Arguments
Syntax:


Public Property PropertyName(ArgList) As ReturnType
Get
Return value
End Get
Set(Byval value as ReturnType)
Assign Value
End Set
End Property


Example : Create Class ClassDemo as below

Public Class ClassDemo
'Data
Private x As Integer
Private y As Integer

'Property with Arguments
Public Property Fields(ByVal Index As Integer)
As Integer
Get
Select Case Index
Advance VB Programming

Lecturer : Heng Bora Page - 10 -

Case 0 : Return x
Case 1 : Return y
End Select
End Get
Set(ByVal value As Integer)
Select Case Index
Case 0 : x = value
Case 1 : y = value
End Select
End Set
End Property
End Class

- Create Object of ClassDemo for Testing as below
Dim obj As New ClassDemo
obj.Fields(0) = 100
obj.Fields(1) = 300
MsgBox("x=" & obj.Fields(0) & vbTab & _
"y=" & obj.Fields(1))

- The Result As Below



4.5- Default Properties
A type can expose one default property, that is, a property whose name of
property can be omitted when accessing and we can create them with the keyword
Default.

Syntax


Default Public Property ProName(arglist) as DataType
.
End Property





Advance VB Programming

Lecturer : Heng Bora Page - 11 -

Example:

Public Class ClsDefualtProperty
'Data Member
Private x() As String

'Defualt Property
Default Public Property Fields(ByVal index As
Integer) As String
Get
Return x(index)
End Get
Set(ByVal value As String)
ReDim Preserve x(index)
x(index) = value
index += 1
End Set
End Property
End Class

- Create Object of ClsDefualtProperty for Testing as
below

Dim obj As New ClsDefualtProperty
obj.Fields(0) = "ABC"
obj.Fields(1) = "Tiger"
obj.Fields(2) = "Angkor"
MsgBox(obj.Fields(0) & vbCrLf & _
obj.Fields(1) & vbCrLf & _
obj.Fields(2))

Or

Dim obj As New ClsDefualtProperty
obj(0) = "ABC"
obj(1) = "Tiger"
obj(2) = "Angkor"
MsgBox(obj(0) & vbCrLf & _
obj(1) & vbCrLf & _
obj(2))




Advance VB Programming

Lecturer : Heng Bora Page - 12 -

- The Result



4.6- Object Graph (or Property that return Object)

Syntax


Public Class Class1

Public Property PropertyCls1() as DataType
.
End Property
.
End Class


Public Class Class2
Private obj As New Class1
Public ReadOnly Property PerpertyCls2() as Class1
Get
return obj
End Get
End Property
End Class



Example: Suppose we have classes as below

Employee Address
ID
Name
Address
Commune
District
Province
Advance VB Programming

Lecturer : Heng Bora Page - 13 -

a- Create Class ClsAddress

Public Class clsAddress
'Data Member
Private _commune As String
Private _district As String
Private _province As String

'Property

Public Property Commune() As String
Get
Return _commune
End Get
Set(ByVal value As String)
_commune = value
End Set
End Property

Public Property District() As String
Get
Return _district
End Get
Set(ByVal value As String)
_district = value
End Set
End Property

Public Property Province() As String
Get
Return _province
End Get
Set(ByVal value As String)
_province = value
End Set
End Property

End Class

Advance VB Programming

Lecturer : Heng Bora Page - 14 -

b- Create Class ClsEmployee

Public Class Employee
'Data Member
Private _id As String
Private _name As String
Private _address As New clsAddress

'Property
Public Property ID() As String
Get
Return _id
End Get
Set(ByVal value As String)
_id = value
End Set
End Property

Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property

Public ReadOnly Property Address() As clsAddress
Get
Return _address
End Get
End Property
End Class

c - Create Object for Testing as below

Dim objEmp As New Employee()
objEmp.ID = "emp001"
objEmp.Name = "Lyyon"
objEmp.Address.Commune = "Svaypor"
objEmp.Address.District = "Battambang"
objEmp.Address.Province = "Battambang"
Advance VB Programming

Lecturer : Heng Bora Page - 15 -


MsgBox("ID: " & objEmp.ID & vbCrLf & _
"Name: " & objEmp.Name & vbCrLf & _
"Address: " & _
"Commune " & objEmp.Address.Commune & _
" District " & objEmp.Address.District & _
" Province " & objEmp.Address.Province)

d- The Result




4.7- Enumerated Properties
Using an Enum to limit the property values of a class

- Syntax Create Enum


Public Enum EnumName
Field1=VariableConstant1
Field2=VariableConstant2
.
FieldN=VariableConstantN
End Enum


- Example: Create Class Product

Public Class Product
Private _pname As ListProduct
Public Enum ListProduct
ABC = 0
Tiger = 1
Beer = 2
Angkor = 3
End Enum
Advance VB Programming

Lecturer : Heng Bora Page - 16 -

Property pname() As ListProduct
Get
Return _pname
End Get
Set(ByVal value As ListProduct)
_pname = value
End Set
End Property
End Class

- Create Object for testing Enum

Dim obj As New Product()
obj.pname = Product.ListProduct.ABC
MsgBox("You Product is " & obj.pname.ToString)

- The Result




5- Methods
Method : the function or Procedure that makes the object to do or perform some
action or task.
- Sub Procedure
- Function Procedure

Example :
Public Class Class1
Private x() As String
Private k as Integer
Public Sub Add(Byval value As String)
Redim Preserve x(k)
x(k)=value
k = k+ 1
End Sub

End Class
Advance VB Programming

Lecturer : Heng Bora Page - 17 -

Dim obj As New Class1
Obj.Add(A)
Obj.Add(B)
.

6- Method Overloading

Method overloading allows you to do the following:
y Create Method with the same name
y Each one accepting different Numbers of parameters
y Each on accepting different datatype of parameters


Example:

Public Class Class1

Public Sub Sort(int as Integer)
..
End Sub
Public Sub Sort(Str as String)

End Sub


End Class

Public Class Class2

Function Find(id as Integer) As String
..
End Sub
Function Find(id as Integer, name as String) As String

End Sub


End Class2

Advance VB Programming

Lecturer : Heng Bora Page - 18 -

7- Event

Event is declaration of the action when taken upon the object. There are several
steps to create your own event:

1- Inside the class module

Declare the event as Public

Public Event EventName()
Or
Public Event EventName(Arglist)

2- Inside the class Module

Raise or trigger the event

RaiseEvent EventName()
Or
RaiseEvent EventName(Arglist)

3- Outside the class

Create Object with keyword WithEvent

Dim WithEvent obj As New ClassName

4- Outside the class

In the edit code window select the Event_Handle

Private Sub obj_EventName(ArgList) Handles obj.EventName

End Sub



Advance VB Programming

Lecturer : Heng Bora Page - 19 -

Example

- Create Class ClassTest

Public Class ClassTest

Private x() As String
Private k As Integer

Public Event Event1(ByVal value As Integer)
Public Sub Add(ByVal value As String)
ReDim Preserve x(k)
x(k) = value
RaiseEvent Event1(k)
k = k + 1
End Sub

End Class

- In Form Module Create Object Test As Below

Public Class Form1

Dim WithEvents obj As New ClassTest

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles MyBase.Load

obj.Add("A")
obj.Add("B")
End Sub

Private Sub obj_Event1(ByVal value As Integer)
Handles obj.Event1
MsgBox("Add at Index=" & value,
MsgBoxStyle.Information, "Wow")
End Sub

End Class

Advance VB Programming

Lecturer : Heng Bora Page - 20 -


- The Result

You might also like