Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.
-
Upload
patience-norris -
Category
Documents
-
view
245 -
download
1
Transcript of Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.
![Page 1: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/1.jpg)
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic .NET, Second Edition
![Page 2: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/2.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 2
Classes and ObjectsLesson A Objectives
• Define a class
• Add properties to a class
• Instantiate an object from a class that you define
![Page 3: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/3.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 3
Defining a Class
• Classes that you define must specify the properties and methods of the objects they create
– Properties describe the characteristics of the objects
– Methods specify the tasks that the objects can perform
![Page 4: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/4.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 4
Defining a Class (continued)
• Use the Class statement to define a class in Visual Basic .NET
• Enter the Class statement in a class file
• After defining a class, you can use it to create objects
![Page 5: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/5.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 5
Defining a Class (continued)
Figure 11-3: Syntax and an example of the Class statement
![Page 6: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/6.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 6
Defining a Class (continued)
Figure 11-4: Procedure for adding a class file to a project
![Page 7: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/7.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 7
Defining a Class (continued)
Figure 11-7: Syntax and examples of creating an object from a class
![Page 8: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/8.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 8
Using a Class That Contains Properties Only
• The sales manager at Sweets Unlimited wants an application to allow him to save each salesperson’s name, quarterly sales amount, and quarterly bonus amount in a sequential access file
• The bonus amount is calculated by multiplying the sales amount by 5%
![Page 9: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/9.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 9
Using a Class That Contains Properties Only (continued)
Figure 11-8: Sample run of the Sweets Unlimited application
![Page 10: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/10.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 10
Using a Class That Contains Properties Only (continued)
• The Salesperson class contains three properties: Name, Sales, and Bonus
• Rules for naming properties
– A name should be composed of one or more words, with the first letter of each word being capitalized
– Use nouns and adjectives to name a property
![Page 11: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/11.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 11
More on Classes and Objects Lesson B Objectives
• Add Property procedures to a class
• Create constructors
• Add methods to a class
![Page 12: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/12.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 12
Using a Class That Contains Properties and Methods
• You will learn how to create a class named Square and use the class in the Area application
• Square class
– Contains one property and two methods
– Creates an object that calculates and returns the area of a square, using the side measurement provided by the application
![Page 13: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/13.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 13
Using a Class That Contains Properties and Methods (continued)
Figure 11-11: Sample run of the Area application
![Page 14: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/14.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 14
Using a Class That Contains Properties and Methods (continued)
• An application cannot directly refer to a Private variable in a class; it must refer to the variable indirectly, through the use of a Public property
• You create a Public property using a Property procedure
![Page 15: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/15.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 15
Using a Class That Contains Properties and Methods (continued)
Figure 11-13: Syntax and an example of creating a Property procedure
![Page 16: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/16.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 16
Using a Class That Contains Properties and Methods (continued)
Figure 11-13: Syntax and an example of creating a Property procedure (continued)
![Page 17: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/17.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 17
Using a Class That Contains Properties and Methods (continued)
• Within the Property procedure you define a Get block of code and a Set block of code
• Code in the Get block allows an application to retrieve the contents of the Private variable associated with the property
• Code in the Set block allows the application to assign a value to the Private variable associated with the property
![Page 18: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/18.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 18
Using a Class That Contains Properties and Methods (continued)
• The Get block uses the Get statement, which begins with the keyword Get and ends with the keywords End Get
• The Set block uses the Set statement, which begins with the keyword Set and ends with the keywords End Set
![Page 19: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/19.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 19
Constructors
• A constructor is a method whose instructions the computer processes, automatically, each time an object is created (instantiated) from the class
• The purpose of a constructor is to initialize the variables of the class
![Page 20: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/20.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 20
Constructors (continued)
Figure 11-14: Syntax and an example of creating a constructor
![Page 21: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/21.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 21
Constructors (continued)
• Every class should have at least one constructor
• A constructor that has no parameters is called the default constructor
• Each constructor included in a class has the same name, New, but its parameters (if any) must be different from any other constructor in the class
![Page 22: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/22.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 22
Methods Other Than Constructors
• Methods, other than constructors, included in a class can be either Sub procedures or Function procedures
• Rules for naming methods
– Names should be composed of one or more words, with the first letter of each word being capitalized
– The first word in a name should be a verb; subsequent words should be nouns and adjectives
![Page 23: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/23.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 23
Methods Other Than Constructors (continued)
Figure 11-15: Syntax and an example of creating a method that is not a constructor
![Page 24: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/24.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 24
Coding the Cornwall Calendars Application
Lesson C Objectives
• Create a class that contains more than one constructor
• Include data validation in a class
![Page 25: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/25.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 25
Using a Class That Contains Two Constructors and Data
Validation• MyDate class
– Contains more than one constructor
– Performs data validation
– Creates an object that returns a month number, followed by a slash, and a day number
• MyDate class is used in the Personnel application
![Page 26: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/26.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 26
Using a Class That Contains Two Constructors and DataValidation (continued)
Figure 11-18: Sample run of the Personnel application
![Page 27: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/27.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 27
Using a Class That Contains Two Constructors and DataValidation (continued)
• MyDate class contains:
– Two Private variables: monthNum and dayNum
– Two Property procedures: Month and Day
– Three methods: two named New and one named GetNewDate
![Page 28: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/28.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 28
Coding the Cornwall Calendars Application
• Cornwall Calendars application
– Created for Jesse Washington, the manager of the Accounts Payable department at Cornwall Calendars
– Should allow Jesse to record (in a sequential access file) the check number, date, payee, and amount of each check written by his department
![Page 29: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/29.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 29
Coding the Cornwall Calendars Application (continued)
Figure 11-21: Interface for the Cornwall Calendars application
![Page 30: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/30.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 30
Coding the Cornwall Calendars Application (continued)
Figure 11-22: TOE chart for the Cornwall Calendars application
![Page 31: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/31.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 31
Coding the Cornwall Calendars Application (continued)
Figure 11-22: TOE chart for the Cornwall Calendars application (continued)
![Page 32: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/32.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 32
Creating the Check Class
• The Check class will contain four properties and two methods
• The Cornwall Calendars application will use the Check class to create a Check object
– It will store the user input in the object’s properties
– It will use the object’s methods to initialize the Private variables and save the check information to a sequential access file
![Page 33: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/33.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 33
Creating the Check Class (continued)
Figure 11-23: Pseudocode for the Check class
![Page 34: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/34.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 34
Coding the uiSaveButton Click Event Procedure
Figure 11-31: Pseudocode for the uiSaveButton’s Click event procedure
![Page 35: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/35.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 35
Coding the uiSaveButton Click Event Procedure (continued)
• The selection structure in the pseudocode determines whether the user entered the check information—in this case, the check number, date, payee, and amount
• If the user neglected to enter one or more of the items, the selection structure’s false path should display an appropriate message
![Page 36: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/36.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 36
Coding the uiSaveButton Click Event Procedure (continued)
Figure 11-34: Message box that appears when the user does not enter all of the check information
![Page 37: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/37.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 37
Summary
• Two versions of the syntax used to create (instantiate) an object from a class:
– Version 1: {Dim | Private} objectVariable As class objectVariable = New class
– Version 2:{Dim | Private} objectVariable As New class
• To access the properties of an object, use the syntax: objectVariable.property
![Page 38: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/38.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 38
Summary (continued)
• To create a Public property, use a Property procedure
– The Get block allows an application to retrieve the contents of the Private variable associated with the property
– The Set block allows an application to assign a value to the Private variable associated with the property
![Page 39: Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.](https://reader036.fdocuments.net/reader036/viewer/2022062313/56649cc95503460f94991667/html5/thumbnails/39.jpg)
Programming with Microsoft Visual Basic .NET, Second Edition 39
Summary (continued)
• A constructor that has no parameters is called the default constructor
• To include more than one constructor in a class, each constructor’s parameters must be different from any other constructor in the class
• To include data validation in a class, place the data validation in the Set block of a Property procedure