Top Banner
OOP-Creating Object- Oriented Programs
41

OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

OOP-Creating Object-Oriented Programs

Page 2: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Objects

• Object - like a noun, a thing– Cmd buttons, text boxes, Recordsets

• Properties - like an adjective, characteristics of object– Caption, text, listindex

• Methods - like a verb, an action or behavior of object– Clear, movenext, additem

• Events - object response to user action or other events– Click, got focus, activate

Page 3: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Class Modules

• VB allows programmers to create new object types in a class module

• Project, Add Class Module– Use "C" as prefix for class module

• In the class module you define– Properties– Methods– Event

Page 4: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Defining a New Class

• Defining your new class is like creating a new tool for the toolbox – the process does not create the object, only a definition of what the object looks like and how it will behave.

• You may then create as many instances of the class as you like

• e.g. your class may be employee, student, product etc

Page 5: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Classes & Instances

• Items in the toolbox represent Classes• When you add a toolbox item to your project - you

add an Instance of the item's Class– Ex. Each textbox on the form is an instance of the textbox

class

• Use the Object Browser (F2) to examine Classes & their associated Properties, Methods, and Events

Page 6: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

ComboBox is selected object

Method symbol

Property symbol

Event symbol

Description

Page 7: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Cookie Cutter Analogy

• Class = Cookie cutter• Instantiate = Make a new cookie with the

cutter• Instantiated cookies are not all "exactly" the

same but they share common characteristics, actions which can be done to them

Page 8: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

this represents the class Textbox

these represent objects—instances of the class

Page 9: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

VB & Object Oriented Programming

• Big advantage of OOP is the ability to reuse objects.

• Reusing objects saves time and effort• You can create three objects from the same

class, yet you can set their properties differently.

Page 10: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

OOP Characteristics

• Object Oriented Programming (OOP) says that a true object oriented language has the following three characteristics;

1) encapsulation, 2) polymorphism, 3) inheritance.

Page 11: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Object Oriented Terminology

• Encapsulation– Combination of characteristics of an object along

with its behavior in "one package"– Cannot make object do anything it doesn't already

"know" how to do– Sometimes referred to as data hiding

Page 12: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Object Oriented Terminology

• Polymorphism– Different classes of objects may have behaviors

that are named the same but are implemented differently

– Programmers can request an action without knowing exactly what kind of object they have or exactly how it will carry out the action

– Ex. Debug.Print, Printer.Print

Page 13: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Object Oriented Terminology

• Inheritance– Ability to create a new class from an existing

class– VB6 cannot do this so we do not consider it

true OOP (Object Oriented Programming)– The next release of VB scheduled for release in

2001, VB.Net, is true OOP since it can handle inheritance and polymorphism

Page 14: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Object Oriented Terminology

• Reusability– The purpose behind Inheritance– VB doesn't exactly allow this– With VB reusability is implemented through • Delegation AND• Superclasses-a base class whose shared code you can

call

Page 15: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Class Design - Analyze:

• Characteristics of your new objects– Characteristics will be properties– Define the properties as variables in the class module

• Behaviors of your new objects– Behaviors will be methods– Define the methods as sub procedures and functions

in the class module

Page 16: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Create a New Class

• Project, Add Class Module• Name with "C" prefix• Define the class properties• Create the class events

Page 17: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

• Define a new class module:– Open a new project– Select Add Class Module from the Project menu– Click the New tab and choose Class Module; click Open– Change the class name to one of your choosing (CProduct in this case)

new name

class module selection

New tab

Page 18: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Properties of a Class

• Declare inside the Class Module in General Declarations

• Do not make Public-that would violate encapsulation (each object should be in charge of its own data)

Private mintPatientNum as IntegerPrivate mdtmDate as DatePrivate mstrLastName as StringPrivate mcurBalance as Currency

Page 19: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Assign Values to Properties

• Write special property procedures (Tools, Add Procedure,Property) to– Pass the values to the class module– Return values from the class module

• Property Let procedure– Sets properties

• Property Get procedure– Retrieves properties from a class– Like a function must return a value

Page 20: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Property Get

Property Get ProcedureName([Optional argument list] [As DataType] )

Statements inside procedureProcedureName=PropertyName

End Property

Example: (remember, Get must return a value)Property Get LastName () as String

' Retrieve the current valueLastName=mstrLastName

End PropertyDelared in Gen Dec of Class Module

Page 21: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Property Let

Property Let ProcedureName([Optional argument list,] Incoming Value [As DataType] )

Statements inside procedurePropertyName=IncomingValue

End PropertyExample:

Property Let LastName () as String' Assign the property valuemstrLastName=strLastName

End Property

Page 22: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Instantiating - Creating a New Object Based on a Class

• Create an instance of the class by using Dim with the New keyword and specify the class with the As keyword in General Declarations

• OR, use Dim in General Declarations and Set Statement in Form_Load

Page 23: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Examples of Creating Instance

Dim|Public|Private variablename As New classnameEx.

Dim mEmployee As New CEmployeePrivate mInventory As New CInventory

OR Ex.

Dim mEmployee As CEmployeeSet mEmployee=New CEmployee

Private mInventory As CInventorySet mInventory=New CInventory

Page 24: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Freeing Resources

• When you are finished with the new object instance you created you should free the resources assigned to it

• Form_Unload is often a good location• Set the new instance of the object equal to

Nothing keyword– Set mEmployee = Nothing

Page 25: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Initialize & Terminate Events

• Each Class Module has two predefined events you can utilize

• Class_Initialize– Triggered when an object is created

• Class_Terminate– Triggered when an object is Set equal to Nothing

or goes out of scope(ex. declared as local in a sub procedure)

Page 26: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Generating Events

• Most objects generate events • You can create objects that generate events• Objects that Generate events are referred to as:– Event Source or Event Provider

• Exs. of events we are used to seeing generated: Click, MouseUp, ADO's WillChangeRecord

Page 27: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Responding to Events

• Forms generally respond to events– Objects that respond to events are referred to as:• Event Sink or Event Consumer

• Examples of events we are used to seeing as responding– cmdOK_Click– form_MouseUp

Page 28: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Event Examples

• User clicks a command button– Event Source(Provider)=the Command Button

• Form module's command button's click event executes– Event Sink(Consumer)=Form

Page 29: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

How to generate an event

• Declare the event in the General Declarations section of the class module, pass arguments with an event if you wish

Public Event TaskComplete( )

• Raise the event in code in the same module the Event was delcared in

If mblnJobFinished Then RaiseEvent TaskComplete

End If

Page 30: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

How to respond to an event

• Declare the object using WithEventsPrivate WithEvents mMyTask as CMyTask

• Instantiate the object using SetSet mMyTask=New CMyTask

• Write the code for the event procedure• When finished release the object variable using

Nothing keyword

Page 31: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Collections

• A Collection Class holds references for a series of objects created from the same class or from different classes

• Actually a collection holds references to the objects– You reference by Index Number or a Key– Similar to list box and the associated items in the

list box

Page 32: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Key for Collection Objects

• Key must be a string• Can be used to reference individual objects in

the collection• Declare the Key as String at the module level

of the Class module for the object (not the collection)

• Add Property Get and Let procedures

Page 33: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Creating a Collection• Create a new class module• Name it as plural of the objects in the

collection– CProducts is a Collection of CProduct objects

• Declare an object variable "As Collection" (in Gen Declarations) and VB automatically provides:– Add, Remove, and Item methods– Count property

Page 34: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Creating a collection cont.

• Code the Class_Initialize Event– Set equal to New Collection

• Code the Class_Terminate Event– Set equal to Nothing

• Code the private function that calculates the next Item number and/or assigns the Key

Page 35: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Creating a collection cont.

• Code the Add Wrapper Event to add items to the collection

• Code the Remove Wrapper Event to remove items to the collection

• Code Item Wrapper Event to access individual elements in the collection

• Write Property Get and Let for the Count property of the collection

Page 36: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Object Browser

• Use it to view the properties, methods, events and constants of VB objects

• It is more complete than Help but less descriptive

• Use it to view the new objects you created in your project

Page 37: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Multitier Applications

• Common use of classes is to create multitier apps

• Each of the functions of a multitier app can be coded in a separate component and stored and run of different machines

• Goal is to create components that can be combined and replaced

Page 38: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Three-tier Applications

• 3-tier is the most common implementation of multitier

• Tiers– User Services– Business Services– Data Services

Page 39: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

User Services Tier

• User Interface– VB Forms– Controls– Menus

Page 40: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Business Services Tier

• All Business Logic• May be written in multiple classes• Generally includes– Data Validation– Calculations– Enforcement of Business Rules

Page 41: OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text boxes, Recordsets Properties - like an adjective, characteristics.

Data Services

• Retrieval and Storage of Data• Databases• Sequential Files• Random Access Files