Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7: Sub and Function Procedures
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
OOP-Creating Object-Oriented Programs
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 4 - Visual Basic Schneider
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
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)
Multiple Document Interface (MDI) application
Chapter 7: Sub and Function Procedures
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
Introduction to Class Modules Please use speaker notes for additional information!
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Multiple Forms, Standard Modules, And Menus
Copyright © 2001 by Wiley. All rights reserved. Chapter 12: Programmer- Defined Types, Direct Access Files, and Object Classes Programmer Defined Data.
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 8: Writing Graphical User Interfaces
8-1 Chapter 8 Using User-Defined Data Types and Object Oriented Programming.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
Classes and Objects noun A word used to denote or name a person, place, thing, quality, or act. verb That part of speech that expresses existence,
Chapter 11 - VB 2005 by Schneider1 Chapter 11 – Object-Oriented Programming 11.1 Classes and Objects 11.2 Arrays of Objects; Events; Containment 11.3 Inheritance.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
 2002 Prentice Hall. All rights reserved. 1 Chapter 9 – Object-Oriented Programming: Inheritance Outline 9.1Introduction 9.2 Base Classes and Derived.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
SYSTEMSDESIGNANALYSIS 1 OO: Chapter 9 Visual Basic: Building Components Jerry Post Copyright © 1999.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
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.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
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.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
IT293 Intermediate Visual Basic Programming Unit 8: Object-oriented I Sydney Liles AIM: sydneyliles.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
‘Tirgul’ # 5 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #5.
Object-Oriented Programming Classes and Objects 11.2 Working with Objects 11.3 Inheritance.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Chapter 11 – Object-Oriented Programming
Chapter 1: An Introduction to Visual Basic 2015
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Chapter 11 – Object-Oriented Programming
Chapter 4 - Visual Basic Schneider
Object-Oriented Programming
Chapter 8 - Functions and Functionality
Presentation transcript:

Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming

Chapter 13 Copyright 2000 All rights reserved 2 Outline and Objectives Classes and Objects Collections of Events Class Relationships

Chapter 13 Copyright 2000 All rights reserved 3 Object-oriented Programming The most important object-oriented term is a class A class is a template from which objects are created A class defines the properties and methods associated with an object An object is defined by a class Creating an object from a class is called instantiation; an object is an instance of a particular class Multiple objects can be created from the same class

Chapter 13 Copyright 2000 All rights reserved 4 Object-oriented Programming A class is a template from which objects are created. Bank-Account John’s Account My Account David’s Account class objects

Chapter 13 Copyright 2000 All rights reserved 5 Creating Objects in VB An object can be created using the following statements: –Dim objectName As New className ‘ in General declarations section OR –Private objectName As className ‘ in General declarations section –Set objectName = New className ‘i n procedure

Chapter 13 Copyright 2000 All rights reserved 6 Accessing Properties of the Object Assign a value to a property: –objectName.propertyName = value Displays the value of a property: –picBox.Print objectname.propertyName Carry out a method –objectName.methodName(arg1,……) Raise an event –RaiseEvent eventName…..

Chapter 13 Copyright 2000 All rights reserved 7 Steps to Create a Class ¶Identify a thing in your program that you would like to represent as an object ·Determine the properties and methods you would like the object to have. ¸Add a class module from the Project menu ¹Set the name property of the class. A common convention is to begin the name with the letter C. ºFor each of the properties in Step 2, declare a private member variable with a statement of the form Private m_variableName As dataType

Chapter 13 Copyright 2000 All rights reserved 8 Steps to Create a Class Continues: »For each member variable in Step 5, create one or two public property procedures to retrieve and assign values of the variable. The general forms of the procedure are: Public Property Get ProcedureName() As DataType ProcedureName = m_variablename (Possibly additional code.) End Property Public Property Let Procedurename(ByVal vNewValue As DataType) m_variableName = vNewValue (Possible additional code.) End Property

Chapter 13 Copyright 2000 All rights reserved 9 Steps to Create a class Continues: ¼For each method in step 2, create a Sub procedure or Function procedure to carry out the task.

Chapter 13 Copyright 2000 All rights reserved 10 The Initialize Event Procedure The object drop-down combo box in a class module window displays two items, General and Class. When you click on Class, the following template appears: Private Sub Class_Initialize() End Sub This procedure is used to set the default values for instance variables and to create other objects associated with this object.

Chapter 13 Copyright 2000 All rights reserved 11 Example of a Circle Class Private m_x As Integer 'Distance from center of circle to left side of form Private m_y As Integer 'Distance from center of circle to top of form Private m_r As Single 'Radius of circle Private Sub Class_Initialize() 'Set the initial center of the circle to the upper 'left corner of the form and set its radius to 500. m_x = 0 m_y = 0 m_r = 500 End Sub

Chapter 13 Copyright 2000 All rights reserved 12 Property Procedures for Class Circle Public Property Get Xcoord() As Integer ‘returns the x coordinate of a circle Xcoord = m_x End Property Public Property Let Xcoord(ByVal vNewValue As Integer) ‘ assigns a value to x m_x = vNewValue End Property Public Property Get Ycoord() As Integer ‘ returns the y coordinate of a circle Ycoord = m_y End Property Public Property Let Ycoord(ByVal vNewValue As Integer) ‘assigns a value to y m_y = vNewValue End Property

Chapter 13 Copyright 2000 All rights reserved 13 Sub procedures to move a circle Public Sub Show() 'Display the circle. 'See discussion of Circle method in Section frmCircles.Circle (m_x, m_y), m_r End Sub Public Sub Move(Dist) 'Move the center of the circle Dist twips to the right 'and Dist twips down. m_x = m_x + Dist m_y = m_y + Dist Call Show End Sub

Chapter 13 Copyright 2000 All rights reserved 14 Example (Moving a circle on the form by invoking the program 5 times)

Chapter 13 Copyright 2000 All rights reserved 15 Collections A collection is an entity, similar to an array, that is especially well-suited to working with sets of objects. A collection is declared with statement of the form: Dim collectionName As new Collection

Chapter 13 Copyright 2000 All rights reserved 16 Collections To add objects to the collection use the statement: –collectionName.Add objectName To delete the nth object from the collection use the statement: –collectionName.Remove n To find out the number of objects in the collection use the statement: –collectionName.Count

Chapter 13 Copyright 2000 All rights reserved 17 Collections: To find the value of a property in the nth object of the collection use the statement: –collectionName.Item(n).propertyName To run the method for the nth object of the collection use the statement: –collectionName.Item(n).methodName

Chapter 13 Copyright 2000 All rights reserved 18 Events In addition to the two pre-defined events for classes, Initialization and Terminate, other user-defined events can be defined.

Chapter 13 Copyright 2000 All rights reserved 19 User-Defined Events To trigger the event place the following statement in the class module in the Declaration section of General –Public Event UserDefinedEvent(arg1, arg2,…..) Place the following statement at the locations in the class module code at which the event should be trigged. –RaiseEvent UserDefinedEvent(arg1, arg2,……..) In the form code, an instance of the class, call it object1, must be declared with a statement of the type –Private WithEvents object1 As CClassName

Chapter 13 Copyright 2000 All rights reserved 20 Example (the event is triggered when the center of the circle changes) Private WithEvents round As CCircle Private Sub Form_Load() Set round = New CCircle End Sub Private Sub round_PositionChanged(x As Integer, y As Integer, r As Single) ‘ This event is triggered when the center of the circle changes. If (x + r > frmCircles.Width) Or (y + r > frmCircles.Height) Then lblCaution.Caption = "Circle Off Screen" frmCircles.ForeColor = QBColor(12) 'Make future circles red End If End Sub User Defined Event The object which will respond to the event

Chapter 13 Copyright 2000 All rights reserved 21 Example of User-defined event Place the following line in the Declaration section of General in the class module. Public Event PositionChanged(a As Integer, b As Integer, c As Single)

Chapter 13 Copyright 2000 All rights reserved 22 Class Relationship The three relationships between classes are “use”, “containment”, and “inheritance”. One class uses another class if it manipulates objects of that class. Class A contains class B when a member variable of class A has class B as its type Inheritance is a process by which one class (the child class), inherits the properties, methods and event of another class (the parent class).