Presentation is loading. Please wait.

Presentation is loading. Please wait.

Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.

Similar presentations


Presentation on theme: "Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San."— Presentation transcript:

1 Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San Diego

2 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Ch. 9: Creating Object Oriented Programs  Object oriented (OO) terminology  Class vs. object  Instantiate an object in a project  Understand Class_Initialize / Terminate events  Create an object collection  Use Object Browser  Add properties to a form

3 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill VB & Object Oriented Programming  Objects have properties, methods, and generate events  Classes have been predefined up until now –Text box class –Form or command button class  VB supports writing object oriented (OO) programs

4 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill 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.

5 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Classes  Design new behaviors and properties for classes you create  You assign property values in your class  You assign properties or reveal them with Property Get and Property Let procedures

6 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Creating a New Class 1) Define a new class module 2) Define the class properties 3) Add property procedures 4) Create a method 5) Save the class module

7 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Defining Class Properties  Declare properties of a class with variables (called instance variables)  Variables are private and placed in General Declaractions section  Add property Let and Get procedures for each instance variable  Code a method, which is a class’ behavior

8 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Creating a New Object Using a Class  Creating a class module defines a new class  Code that creates and uses objects is placed in form and code modules, whereas a class must be placed in a class module  Create an object of a class with the New keyword—called instantiating an object  The New keyword creates a new instance of a class when the object is first used

9 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Choosing When to Create New Objects  The Set statement is another way to instantiate an object  Program has more control over when object is created with the Set statement  Form: Private As Set = New  Set statement is assignment statement for object variables

10 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Initialize and Terminate Events  VB uses class two special procedures for class definitions: –Class_Initialize –Class_Terminate  Class_Initialize method is invoked each time an instance of a class is created  Class_Terminate method is invoked each time an instance of a class is destroyed

11 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Terminating Projects  When a project terminates, use the Unload statement to unload all forms before executing the End statement  Unloading forms gives VB a chance to release resources and invoke the Class_Terminate event  Unload all forms using a For Each loop (see notes)

12 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Collections  VB provides a collection, which stores a group of individual objects  Collection can be treated as a single unit  A collection class contains references of all objects created of the same class  Collection has: Add method, Remove method, Item method, and Count property  Use plural of object to name class: Forms is collection of any form created in project

13 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using a Collection in a Form

14 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using the Object Browser  Object Browser reveals names of objects, properties, methods, events, and VB constants  View objects: View, Object Browser  Object Browser is best way to look up an object’s available properties, methods, etc.  Select an object and press F1 for help

15 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Using a List Box to Store the Keys  Reference collection members with unique keys  Keeping track of keys is difficult  Keys can be stored in a list box & kept in sorted order  List’s ItemData property holds the collection key

16 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill List Box can Display an Object  User selects a name from the list box  Your code uses the ItemData property to reference the correct member of the collection  You populate form text boxes with the selected member’s fields

17 Copyright© 2000 by the McGraw-Hill Companies, Inc.Irwin/McGraw-Hill Hands-On Programming Example  Hands on project manages employees with two forms: –Main form –Employee data form  Main form is used to select employee  Employee data form displays employee data


Download ppt "Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San."

Similar presentations


Ads by Google