OOP-Creating Object-Oriented Programs

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Classes, Exceptions, Collections, and Scrollable Controls
When is Orientated Programming NOT? Mike Fitzpatrick.
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Object Oriented Programming Intermediate VB Doug Waterman Fox Valley Technical College.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
VBA Modules, Functions, Variables, and Constants
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Getting Started with VB .NET
Using Objects and Properties
instance variables property procedures for the mintQuantity instance variable.
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Chapter 13: Object-Oriented Programming
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Apply Sub Procedures/Methods and User Defined Functions
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Introduction to Visual Basic (VB)
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
An Object-Oriented Approach to Programming Logic and Design
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Chapter 2: Creating ActiveX Code Components By นภดล กมลวิลาศเสถียร Dept. of Computer Engineering, Prince of Songkla University Source: Mastering Visual.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
CHAPTER THREE Representing Data: Constants and Variables.
Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Object-Oriented Programming: Classes and Objects.
Multiple Forms and Menus
A variable is a name for a value stored in memory.
Programming Logic and Design Seventh Edition
Chapter 1: An Introduction to Visual Basic 2015
Multiple Classes and Inheritance
Chapter 11 – Object-Oriented Programming
VISUAL BASIC.
CIS16 Application Development Programming with Visual Basic
Programming with Microsoft Visual Basic 2008 Fourth Edition
CS285 Introduction - Visual Basic
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
Presentation 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. this represents the class Textbox these represent objects—instances of the class

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 Integer Private mdtmDate as Date Private mstrLastName as String Private 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 procedure ProcedureName=PropertyName End Property Example: (remember, Get must return a value) Property Get LastName () as String ' Retrieve the current value LastName=mstrLastName Delared in Gen Dec of Class Module

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

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 classname Ex. Dim mEmployee As New CEmployee Private mInventory As New CInventory OR Dim mEmployee As CEmployee Set mEmployee=New CEmployee Private mInventory As CInventory Set 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 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

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