Clearly Visual Basic Programming with Visual Basic 2010
Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition Chapter 26 I Love This Class (Creating a Class)
Objectives After studying Chapter 26, you should be able to: Explain the terminology used in object-oriented programming Create a class Instantiate an object Add Property procedures to a class Include data validation in a class Create a default constructor Include methods other than constructors in a class 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
That’s a Real Classy Object-oriented programming language Allows the programmer to use objects to accomplish a program’s goal Class Pattern or blueprint that the computer follows when creating an object Object Instantiated (created) from a class Referred to as an instance of a class 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
That’s a Real Classy Object (cont’d. ) Attributes Characteristics that describe an object Behaviors Include methods and events Methods Operations (actions) that the object is capable of performing Events Actions to which an object can respond 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 4
That’s a Real Classy Object (cont’d. ) A class Encapsulates all of the attributes and behaviors of the object it instantiates Class statement Used to define a class Pascal case Capitalize the first letter in the name and the first letter in any subsequent words in the name 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 5
Figure 26 -1 Class statement syntax, example, and steps 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 6
Revisiting the Willow Pools Application Willow Pools application Calculates amount of water required to fill a pool Determines the pool’s volume Calculated by multiplying the pool’s length by its width and then multiplying the result by the pool’s depth 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 7
Figure 26 -2 Code for the Willow Pools application using a structure 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 8
Who Owns That Property? When an application instantiates an object: Only the Public members of the object’s class are made available to the application Property procedure Used to create a Public property Figure 26 -4 Code contained in the Get block allows application to retrieve the contents of the Private variable Code in the Set block allows an application to assign a value to the Private variable 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 9
Who Owns That Property? (cont’d. ) Figure 26 -4 Basic syntax and examples of a Public Property procedure (continues) 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 10
Figure 26 -4 Basic syntax and examples of a Public Property procedure (cont’d. ) 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 11
Who Owns That Property? (cont’d. ) Get statement Begins with the keyword Get and ends with the keywords End Get Set statement Begins with the keyword Set and ends with the keywords End Set 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 12
Behave Yourself Objects have attributes and behaviors Behaviors The operations (actions) the object is capable of performing Rectangular pool object behaviors example Initialize its Private variables when instantiated This behavior requires a constructor Calculate its volume Calculate number of gallons of water to fill 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Constructive Behavior Is the Key to Success Constructor Class method whose sole purpose is to initialize the class’s Private variables Always named New Default constructor Constructor that has no parameters A class can have only one 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 14
Figure 26 -6 Syntax and an example of a constructor 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 15
Methods Other than Constructors Class methods other than constructors Can be either Sub procedures or Function procedures Method name Should be entered using Pascal case First word should be a verb, and any subsequent words should be nouns and adjectives 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 16
Figure 26 -7 Syntax and an example of a method that is not a constructor 17 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 17
Using the Pattern to Create an Object After defining a class: It can then be used to instantiate one or more objects Figure 26 -9 Shows two versions of the basic syntax to instantiate an object 18 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 18
Figure 26 -9 Syntax and examples of instantiating an object from a class 19 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 19
Pool Supplies Application Pool Supplies Sells a water clarifier designed to combat a common problem in swimming pools: cloudy water Recommends 1 ounce of So. Clear clarifier per 5000 gallons of water Manager wants an application that calculates the: Number of gallons of water contained in a pool Required number of ounces of clarifier 20 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 20
Figure 26 -13 btn. Calc control’s Click event procedure 21 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Summary Objects used in an object-oriented program Instantiated (created) from classes Class statement Used to define a class When naming the Private variables in a class: Begin the name with the underscore character When an object is instantiated in an application: Public members of the class are exposed to the application 22 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Summary (cont’d. ) Property procedure Used to create a Public property Get block allows an application to access the contents of the Private variable Constructors Purpose: Initialize private variables Class can have one or more constructors Names of methods in a class Should be entered using Pascal case 23 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition 23
- Slides: 23