1 / 41

OOP-Creating Object-Oriented Programs

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

Télécharger la présentation

OOP-Creating Object-Oriented Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP-Creating Object-Oriented Programs

  2. 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

  3. 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

  4. 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

  5. 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

  6. Method symbol ComboBox is selected object Property symbol Event symbol Description

  7. 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

  8. this represents the class Textbox these represent objects—instances of the class

  9. 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.

  10. OOP Characteristics • Object Oriented Programming (OOP) says that a true object oriented language has the following three characteristics; 1) encapsulation, 2) polymorphism, 3) inheritance.

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

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

  17. New tab class module selection new name • 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)

  18. 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 Integer Private mdtmDate as Date Private mstrLastName as String Private mcurBalance as Currency

  19. 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

  20. Delared in Gen Dec of Class Module Property Get Property Get ProcedureName([Optional argument list] [As DataType] ) Statements inside procedure ProcedureName=PropertyName End Property Example: (remember, Get must return a value) Property Get LastName () as String ' Retrieve the current value LastName=mstrLastName End Property

  21. Property Let Property Let ProcedureName([Optional argument list,] Incoming Value [As DataType] ) Statements inside procedure PropertyName=IncomingValue End Property Example: Property Let LastName () as String ' Assign the property value mstrLastName=strLastName End Property

  22. 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

  23. Examples of Creating Instance Dim|Public|Private variablename As New classname Ex. Dim mEmployee As New CEmployee Private mInventory As New CInventory OR Ex. Dim mEmployee As CEmployee Set mEmployee=New CEmployee Private mInventory As CInventory Set mInventory=New CInventory

  24. 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

  25. 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)

  26. 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

  27. 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

  28. 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

  29. 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 TaskCompleteEnd If

  30. How to respond to an event • Declare the object using WithEvents Private WithEvents mMyTask as CMyTask • Instantiate the object using Set Set mMyTask=New CMyTask • Write the code for the event procedure • When finished release the object variable using Nothing keyword

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. Three-tier Applications • 3-tier is the most common implementation of multitier • Tiers • User Services • Business Services • Data Services

  39. User Services Tier • User Interface • VB Forms • Controls • Menus

  40. Business Services Tier • All Business Logic • May be written in multiple classes • Generally includes • Data Validation • Calculations • Enforcement of Business Rules

  41. Data Services • Retrieval and Storage of Data • Databases • Sequential Files • Random Access Files

More Related