Lecture (6)Writing Classes and Modules

28
8/14/2019 Lecture (6)Writing Classes and Modules http://slidepdf.com/reader/full/lecture-6writing-classes-and-modules 1/28    U    N    I    V    E    R    S    I    T    I    K    U    A    L    A    L    U    M    P    U    R    M   a    l   a   y   s    i   a    F   r   a   n   c   e    I   n   s    t    i    t   u    t   e FSB23103 1 FSB23103 Object Oriented Programming Lecture 7 Writing classes and modules Mdm Ratnawati Ibrahim

Transcript of Lecture (6)Writing Classes and Modules

Page 1: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n

s t i t u t e

FSB23103 1

FSB23103 Object Oriented Programming

Lecture 7

Writing classes and modules

Mdm Ratnawati Ibrahim

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 2

Topics

bull Class definitionbull Keyword ME

bull How to write classes

bull What are modules

bull How to write modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 3

Classes

bull A class is defined in terms of its state

behaviour or actions

bull Each class contains

attributes or class instance variables to describe thestate

a set of methods that manipulate the attributes

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 4

Class structure

bull

Class members Class instances variables

(usually Private)

Public methods amp properties

bull Constructor

bull Mutatorsbull Accessors

bull Other actions

Private methods

Note Public ndash accessible from anywhere Private ndash accessible only from within

the class

Public Class className

End Class

Private

Class instance variables

Private methods

Public methods

Constructor

Other actions

Public properties

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 5

Class members

bull Class instances variables - usually Private to store the state of an object

bull Class constructor

to create an object

bull

Class properties to access or change the state of an object

bull Public methods

to change the behaviour of an object

bull

Private methods only accessible within the class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 2: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 2

Topics

bull Class definitionbull Keyword ME

bull How to write classes

bull What are modules

bull How to write modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 3

Classes

bull A class is defined in terms of its state

behaviour or actions

bull Each class contains

attributes or class instance variables to describe thestate

a set of methods that manipulate the attributes

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 4

Class structure

bull

Class members Class instances variables

(usually Private)

Public methods amp properties

bull Constructor

bull Mutatorsbull Accessors

bull Other actions

Private methods

Note Public ndash accessible from anywhere Private ndash accessible only from within

the class

Public Class className

End Class

Private

Class instance variables

Private methods

Public methods

Constructor

Other actions

Public properties

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 5

Class members

bull Class instances variables - usually Private to store the state of an object

bull Class constructor

to create an object

bull

Class properties to access or change the state of an object

bull Public methods

to change the behaviour of an object

bull

Private methods only accessible within the class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 3: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 3

Classes

bull A class is defined in terms of its state

behaviour or actions

bull Each class contains

attributes or class instance variables to describe thestate

a set of methods that manipulate the attributes

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 4

Class structure

bull

Class members Class instances variables

(usually Private)

Public methods amp properties

bull Constructor

bull Mutatorsbull Accessors

bull Other actions

Private methods

Note Public ndash accessible from anywhere Private ndash accessible only from within

the class

Public Class className

End Class

Private

Class instance variables

Private methods

Public methods

Constructor

Other actions

Public properties

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 5

Class members

bull Class instances variables - usually Private to store the state of an object

bull Class constructor

to create an object

bull

Class properties to access or change the state of an object

bull Public methods

to change the behaviour of an object

bull

Private methods only accessible within the class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 4: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 4

Class structure

bull

Class members Class instances variables

(usually Private)

Public methods amp properties

bull Constructor

bull Mutatorsbull Accessors

bull Other actions

Private methods

Note Public ndash accessible from anywhere Private ndash accessible only from within

the class

Public Class className

End Class

Private

Class instance variables

Private methods

Public methods

Constructor

Other actions

Public properties

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 5

Class members

bull Class instances variables - usually Private to store the state of an object

bull Class constructor

to create an object

bull

Class properties to access or change the state of an object

bull Public methods

to change the behaviour of an object

bull

Private methods only accessible within the class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 5: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 5

Class members

bull Class instances variables - usually Private to store the state of an object

bull Class constructor

to create an object

bull

Class properties to access or change the state of an object

bull Public methods

to change the behaviour of an object

bull

Private methods only accessible within the class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 6: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 6

Example ndash Account class

bull

The Account classPublic Class Account

Private balance As Double

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubPublic Sub Deposit(ByVal amount As Double)

balance += amount

End Sub

Public Sub Withdraw(ByVal amount As Double)

balance -= amount

End Sub

Public ReadOnly Property currentBalance() As Double

Get

Return balance

End Get

End Property

End Class

Class instance variable

Constructor

Mutator Method

Assessor

method

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 7: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 7

Special object - Me (1)

bull

Visual Basic Net is an object-oriented programminglanguage

bull When we create a new project a class named Form1 isautomatically created

bull

When we run a program we are running an instance of a class

eg the object ball of the Circle class

the object myAccount of the Account class

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 8: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 8

Special object - Me (2)

bull

In VB the keyword Me refers to the currently running objectbull eg in a class definition we can distinguish between class

instance variables and parameters using the keyword Me

There is no need to choose new names for parameters

Public Sub New(ByVal iniAmount As Double)

balance = iniAmount

End SubParameters

Public Sub New(ByVal balance As Double)

Mebalance = balance

End Sub

Me

object

Class instance

variable

Parameters

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 9: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 9

Example ndash Complex numbers (1)

bull

RequirementsCreate a class named Complex to represent complex numbers

together with three operations Sum Difference and Product Writea test program to demonstrate the capabilities of the class

Complex

Numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 10: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 10

Example ndash Complex numbers (2)

bull

Analysis a complex number consists of two parts

bull real (Double type)

bull imaginary (Double type)

Operations on two complex numbers

bull Sum

bull Difference

bull Producteg two complex numbers

C1 = (x1 y1) C2 = (x2 y2)

Sum = ( x1 + x2 y1 + y2 )

Difference = ( x1 - x2 y1 - y2 )

Product = ( x1 x2 - y1 y2 x1 y2 + x2 y1)

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 11: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 11

Example ndash Complex numbers (3)

bull

The Complex class design State

bull real part

bull imaginary part

Methods

bull Constructor to create objects of the Complex class

bull Sum to return the sum of two complex numbers

bull Difference

to return the difference between two complex numbersbull Product

to return the product of two complex numbers

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 12: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 12

Example ndash Complex numbers (4)

bull

The Complex class implementation Private instance variables

bull real imaginary of Double type

Public constructor

bull to initialize real imaginary

Public properties

bull R - Get and Set real

bull I - Get and Set imaginary

Public methods

bull Function Sumbull Function Difference

bull Function Product

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 13: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 13

Method or property

bull Both methods and properties allow user to access anobject

bull Methods are used to describe actions or behaviours of an object

bull Properties are used to access or modify the state of an object

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 14: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 14

Create a new class

bull In VB Net select Project | Add Class bull eg the Complex class

Note VB convention requires that a class name starts with a capital letter and ends with vb

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 15: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 15

The Complex Class - code (1)

Public Class Complex

lsquo class instance variables

Private real As DoublePrivate imaginary As Double

lsquo class constructor

Public Sub New(ByVal real As Double _

ByVal imaginary As Double)

Mereal = real

Meimaginary = imaginaryEnd Sub

Public class

constructor

Private class

Instance variables

Class declaration

Me

object

Class

instance

variable

Parameter

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 16: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 16

The Complex Class - code (2)

Public Property R() As Double

Get

Return real

End Get

Set(ByVal value As Double)

real = valueEnd Set

End Property

Public Property I() As Double

Get

Return imaginary

End Get

Set(ByVal value As Double)

imaginary = value

End Set

End Property

Public Properties Public

property R

Public

property I

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 17: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 17

The Complex Class - code (3)

Public Function Sum(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR + cR MeI + cI)

Return z

End Function

Public Function Difference(ByVal c As Complex) As Complex

Dim z As Complex

z = New Complex(MeR - cR MeI - cI)

Return z

End Function

Public Function Product(ByVal c As Complex) As ComplexDim z As Complex

z = New Complex(MeR xR - MeI xI _

MeR xI + MeI xR)

return z

End Function

End Class

Public Methods

Function

Sum

Function

Difference

FunctionProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 18: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 18

The test program ndash form design

Command button

cmdSum cmdDiffcmdProduct

cmdClear cmdExit

Textbox

txbX1 txbY1

txbX2 txbY2

Label

lblResult

Input

Output

Operation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 19: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 1928

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 19

Public Class Form1

declare object variables

Dim c1 As Complex

Dim c2 As Complex

Private Sub Form1_Load(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles MyBaseLoad

create amp initialize objects

c1 = New Complex(1 0)

c2 = New Complex(1 0)End Sub

The test program ndash code (1)

Declare Complex

objects c1 c2

Form load

Create and initializeobjects c1 c2

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 20: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2028

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I n s t i t u t e

FSB23103 20

Private Sub cmdAdd_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdSumClick

get2Numbers()

DisplayResult(sum c1Sum(c2))

End Sub

Private Sub cmdDiff_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdDiffClick

getNumbers()

DisplayResult(difference c1Difference(c2))

End Sub

Private Sub cmdProduct_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdProductClick

getNumbers()

DisplayResult(product c1Product(c2))

End Sub

The test program ndash code (3)

Button

cmdSum

Button

cmdDiff

Button

cmdProduct

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 21: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2128

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 21

Private Sub getNumbers()

c1R = CDbl(txbX1Text)

c1I = CDbl(txbY1Text)

c2R = CDbl(txbX2Text)

c2I = CDbl(txbY2Text)

End Sub

Private Sub DisplayResult(ByVal opType As String

ByVal result As Complex)

lblResultText = The amp opType amp is ( _

amp CStr(resultR) amp _

amp CStr(resultI) amp )

End Sub

The test program ndash code (2)

Display

result

Get user

inputs

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 22: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2228

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 22

Private Sub cmdClear_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdClearClick

txbX1Text =

txbY1Text =

txbX2Text =

txby2Text =

lblResultText = End Sub

Private Sub cmdExit_Click(ByVal sender As SystemObject _

ByVal e As SystemEventArgs) _

Handles cmdExitClick

Close()

End SubEnd Class

The test program ndash code (4)

Button

cmdClear

Button

cmdExit

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 23: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2328

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 23

Modules

bull

are used to group related methods so that they can bereused in other projects

bull should be self-contained

ie methods in the module should not require access to variablesand methods outside the module except when such values are

passed as arguments

bull do not have a GUI and can contain only code

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 24: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2428

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 24

Module example

Examplesbull Calculation of the area

and perimeter of differentshapes eg

a circle

a rectanglebull We can create a module

say ShapeMessurements to store all the methodsrequired for the

calculation

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 25: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2528

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 25

Create a module

bull In VB Net select Project | Add Module bull Module file names end with vb bull Variables and methods must be declared Public in order to be

available to other modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 26: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2628

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 26

Module definition - example

Module ShapeMessurements

circle shapePublic Function CircleArea(ByVal r As Double) As Double

Return MathPI r ^ 2

End Function

Public Function CircleCircumference(ByVal r As Double) As

Double

Return 2 MathPI r

End Function

rectangle shape

Public Function RectangleArea(ByVal width As Double _

ByVal height As Double) As Double

Return width heightEnd Function

Public Function RectanglePerimeter(ByVal width As Double _

ByVal height As Double) As Double

Return 2 (width + height)

End Function

End Module

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 27: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2728

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

FSB23103 27

Use module - example

Private Sub cmdCalculate_Click(ByVal sender As SystemObject _ByVal e As SystemEventArgs) _

Handles cmdCalculateClick

get user inputs

Dim r As Double = CDbl(txbRadiusText)

Dim w As Double = CDbl(txbWidthText)

Dim h As Double = CDbl(txbHeightText)

calculate amp display results of the circle

lblCircleAreaText = Format(CircleArea(r) 000)

lblCircumferenceText = Format(CircleCircumference(r) 000)

calculate amp display results of the regtanglelblRecAreaText = Format(RectangleArea(w h) 000)

lblPerimeterText = Format(RectanglePerimeter(w h) 000)

End Sub

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained

Page 28: Lecture (6)Writing Classes and Modules

8142019 Lecture (6)Writing Classes and Modules

httpslidepdfcomreaderfulllecture-6writing-classes-and-modules 2828

U N I V E R S I T I K U A L A

L U M P U R

M a l a y s i a F r a n c e I

n s t i t u t e

S

Summary

bull

The keywordMe

refers to the currently running objectbull Class members

private class instances variables

public class constructor

public methods (subroutines amp functions)

public properties

private methods

bull Modules

are used to group related methods to be reused in other

projects should be self-contained