Download presentation
1
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic .NET, Second Edition
2
Classes and Objects Lesson A Objectives
Define a class Add properties to a class Instantiate an object from a class that you define 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 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 Programming with Microsoft Visual Basic .NET, Second Edition
5
Defining a Class (continued)
Figure 11-3: Syntax and an example of the Class statement 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 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 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% 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 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 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 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 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 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 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 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) 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 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 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 Programming with Microsoft Visual Basic .NET, Second Edition
20
Constructors (continued)
Figure 11-14: Syntax and an example of creating a constructor 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 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 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 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 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 Programming with Microsoft Visual Basic .NET, Second Edition
26
Using a Class That Contains Two Constructors and Data Validation (continued)
Figure 11-18: Sample run of the Personnel application Programming with Microsoft Visual Basic .NET, Second Edition
27
Using a Class That Contains Two Constructors and Data Validation (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 Programming with Microsoft Visual Basic .NET, Second Edition
28
Coding the 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 Programming with Microsoft Visual Basic .NET, Second Edition
29
Coding the Cornwall Calendars Application (continued)
Figure 11-21: Interface for the Cornwall Calendars application 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 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) 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 Programming with Microsoft Visual Basic .NET, Second Edition
33
Creating the Check Class (continued)
Figure 11-23: Pseudocode for the Check class 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 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 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 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 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 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 Programming with Microsoft Visual Basic .NET, Second Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.