Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

Object-oriented Programming

in VBA

n ce p t s
Ba s i c co Adapted from Slides
By Prof. Adrian Holzer

9
(JAVA) With kind

CT A permission
Today

Classes
Objects
Goals
• Understand basic Object programming
concepts
• Be familiar with Objects when working with
Excel’s object model (next lesson)
• Improve the structure of your programs if
you’ll be making extensive use of VBA
Objects

represent particular ’things’ from the


real world, or from some problem
domain (ex: “my blue rocking chair”)
Classes

represent all objects


of a kind
(ex: “chairs”)
Specification vs Implementation

(what it does)

(how it does
it)
Specification viewpoint
Methods & Parameters

rotate()
Methods & Parameters

Methods may have parameters


to pass additional information
needed to execute

Rotate(degrees as integer)
Methods & Parameters

Rotate(degrees
as integer)
Implementation Viewpoint

(how it does
it)
Instances

Many instances (objects)


can be created from a
single class
Properties (fields)
The VBA source code of classes defines the attributes
(Properties / or fields) and methods all objects of the class
have

class Chair
Color as String
Model as String
IsBroken as Boolean
Age as Integer
Property (Field) Values
An object stores its own values for each property /
field
Chair myChair1

color “green”

model “shell”
isBroken false
age 5

the field values represent the object’s state


Two Chair Instances
class Chair
Color as String
Model as String
IsBroken as Boolean

Age as Integer

Chair myChair1 Chair myChair2

color “brown” color “green”


model “wood” model “shell”
isBroken false isBroken false
age 50 age 5
Example
Defining a class module
Class name
You now need to give your class a name
(initially it will be called Class 1) in the
Properties window (F4):
Properties (Fields)

Properties store values of an object’s state.


In VBA, they can be defined through
module-level variables
Public name As String
Public birthDate As Date
Private pGender As String
Public grossSalary As Double
They are also known as
instance variables

Visibility modifier field name type


Visibility Modifiers
public access by any class anywhere

private within class

Default (Dim) private


Creating an object
Once the Class Module has been defined, it can be used as a
new type in order to create an instance

Sub test()
Dim myEmployee As CEmployee

Set myEmployee = New CEmployee

Creation of a new
object
Using properties
Properties are accessed / modified in the
same way as fields of a user-defined type

myEmployee.name = "Paul"
myEmployee.birthDate = CDate("03.03.1966")
myEmployee.grossSalary = 100000

MsgBox myEmployee.name

myEmployee.pGender = "M" Not allowed on


private variables !
Properties :
alternative
Properties can also be defined through Property procedures
(Get & Let (or Set)). This allows to have more control on the
object

Property Get gender() As String For Object properties, you


gender = pGender
End Property must use
Property Let gender(ByVal theGender As String) Property Set (instead of Let)
If theGender = "M" Or theGender = "F" Then
pGender = theGender
Else
MsgBox "Undefined gender : " & theGender & " F or M to be used"
End If
End Property
Properties :
alternative(II)
Property Get allows you to have “computed” fields or read-
only fields

Property Get currentAge() As Double


currentAge = WorksheetFunction.Days360 _
(birthDate, Date) / 360
End Property

No property Let : currentAge


is “read only” (and not stored)
Methods
Methods implement the behavior of objects

Public Sub AdaptSalaryToCostOfLiving(rate As Double)

Me.grossSalary = Me.grossSalary * (1 + rate)


The header defines the End Sub

method’s signature

The body encloses the method’s statements


Method

Methods can be either subs or functions

Public Function ComputeLPPAge(valuationYear As Integer) As Integer


ComputeLPPAge = valuationYear - Year(Me.birthDate)
End Function

They are called in the same way as properties


age = myEmployee.ComputeLPPAge(2012)
myEmployee.AdaptSalaryToCostOfLiving (0.01)
Local Variables
So Far
So Far
Abstraction

Abstraction is the ability to ignore


details of parts to focus attention
on a higher level of a problem
Modularization

Modularization is the process of


dividing a whole into well-defined
parts, which can be built and examined
separately, and which interact in well-
defined ways
Specification

Behavior of a typical ticket machine:


Machines supply tickets of a fixed price

Q:
How is that price determined?
Q:How is ‘money’ entered into a machine?
Q:How does a machine keep track of the money that is entered?
Specification

Interacting with an object gives


us clues about its behavior

(what it does)
Implementation

Looking inside its class allows us to


determine how that behavior is provided or
implemented

(how it does
it)
References

[Seref 07] Michelle M.H. Şeref, Ravindra K. Ahuja, and Wayne L. Winston ;
Developing Spreadsheet-Based Decision Support Systems Using Excel and
VBA for Excel ; Dynamic Ideas, Belmont, Massachusetts 2007

http://msdn.microsoft.com/en-us/library/office/aa164936(v=office.10).aspx

You might also like