+ All Categories
Home > Documents > OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text...

OOP-Creating Object- Oriented Programs. Objects Object - like a noun, a thing – Cmd buttons, text...

Date post: 19-Dec-2015
Category:
View: 220 times
Download: 0 times
Share this document with a friend
Popular Tags:
41
OOP-Creating Object- Oriented Programs
Transcript

OOP-Creating Object-Oriented Programs

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

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

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

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

ComboBox is selected object

Method symbol

Property symbol

Event symbol

Description

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

this represents the class Textbox

these represent objects—instances of the class

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.

OOP Characteristics

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

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

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

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

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

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

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

Create a New Class

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

• 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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

Three-tier Applications

• 3-tier is the most common implementation of multitier

• Tiers– User Services– Business Services– Data Services

User Services Tier

• User Interface– VB Forms– Controls– Menus

Business Services Tier

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

Data Services

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


Recommended