22
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Embed Size (px)

DESCRIPTION

Clearly Visual Basic: Programming with Visual Basic 2008 That’s a Real Classy Object 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

Citation preview

Page 1: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 27I Love this Class

Page 2: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Objectives

• Define a class• Instantiate an object from a class that you define• Add Property procedures to a class• Include data validation in a class• Create a default constructor• Include methods in a class

2

Page 3: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

That’s a Real Classy Object

• 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

Page 4: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

That’s a Real Classy Object (continued)

• 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

Page 5: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

That’s a Real Classy Object (continued)

• 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

Page 6: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 6

Page 7: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Revisiting the Willow Pools Application

• Willow Pools application– Calculates and displays both the volume of a

rectangular pool and the amount of water required to fill the pool

– Volume • Calculated by multiplying the pool’s length by its width

and then multiplying the result by the pool’s depth

7

Page 8: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 8

Page 9: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

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 27-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

Page 10: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 10

Page 11: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 11

Page 12: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Who Owns that Property? (continued)

• 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

Page 13: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Constructive Behavior is the Key to Success

• Constructor – Class method whose instructions are processed by

the computer when an object is instantiated– Responsible for creating the object and initializing

the class’s Private variables• Default constructor

– Constructor that has no parameters– A class can have only one

13

Page 14: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 14

Page 15: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Methods Other than Constructors

• Methods included in a class – 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

15

Page 16: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 16

Page 17: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Using the Pattern to Create an Object

• After defining a class:– It can then be used to instantiate one or more

objects• Figure 27-9

– Shows two versions of the basic syntax to instantiate an object

17

Page 18: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 18

Page 19: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008 19

Page 20: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Pool Supplies Application

• Pool Supplies – Sells a water clarifier designed to combat a common

problem in swimming pools: cloudy water– Recommends one ounce of SoClear 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

Page 21: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

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

21

Page 22: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class

Clearly Visual Basic: Programming with Visual Basic 2008

Summary (continued)

• Property procedure– Used to create a Public property – Get block allows an application to access the

contents of the Private variable • Class

– Can have one or more constructors• Names of methods in a class

– Should be entered using Pascal case

22