Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming."— Presentation transcript:

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

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

3 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

4 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

5 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

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

7 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

8 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

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

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

11 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

12 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

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

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

15 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

16 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

17 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

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

19 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

20 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

21 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)

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


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

Similar presentations


Ads by Google