50
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance

CHAPTER ELEVEN

  • Upload
    adie

  • View
    24

  • Download
    0

Embed Size (px)

DESCRIPTION

CHAPTER ELEVEN. Multiple Classes and Inheritance. Objectives. Use the TabIndex Property Edit input, including MaskedTextBox,TextBox , and ComboBox objects Describe the three-tier program structure Understand a class Create a class. Objectives. Instantiate an object - PowerPoint PPT Presentation

Citation preview

Page 1: CHAPTER ELEVEN

Microsoft Visual Basic 2008

CHAPTER ELEVEN

Multiple Classesand Inheritance

Page 2: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 2

Objectives

►Use the TabIndex Property►Edit input, including MaskedTextBox,TextBox, and

ComboBox objects►Describe the three-tier program structure►Understand a class►Create a class

Page 3: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 3

Objectives

►Instantiate an object►Pass arguments when instantiating an object►Write a class constructor►Call a procedure in a separate class

Page 4: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 4

Objectives

►Code a base class and a subclass incorporating inheritance

►Call procedures found in a base class and a subclass

►Write overridable and overrides procedures►Create and write a comma-delimited text file

Page 5: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 5

Preview the Chapter Project

Page 6: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 6

User Interface and the TabIndex Property

►Select the object that will be selected when program execution begins. In the example, this is the txtStudentID MaskedTextBox object. Scroll in the Properties window until the TabIndex property is visible and then double-click in the right column of the TabIndex property

►Type 1 and then press the ENTER key►Select the object which should be selected when the user

presses the Tab key. In the sample program, the txtStudentName TextBox object should be selected. Double-click the right column of the TabIndex property for the txtStudentName TextBox object, type 2 and then press the ENTER key

Page 7: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 7

User Interface and the TabIndex Property

Page 8: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 8

User Interface and the TabIndex Property

Page 9: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 9

Editing Input Data

► Student ID: The student ID object is a masked text box and the mask is for the social security number, so the mask ensures that the user can enter only numbers. But, the social security mask does not ensure the user enters all nine numbers. Therefore, a check must be included in the program to require the user to enter all 9 numeric digits.

► Student Name: The program must ensure the user enters characters in this TextBox object. In addition, spaces cannot be entered instead of actual alphabetic characters.

► Number of Units: The user must enter a numeric value from 1 through 24 for the number of units the student is taking.

► Major: The user must select a major from the list in the Major ComboBox object.

Page 10: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 10

Editing Input Data

Page 11: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 11

Program Structure Using Classes

►The concept of separating processing and hiding data within specific classes is called encapsulation

►When developing programs with multiple classes, a starting point for determining what classes should appear in a program is the three-tier program structure

Page 12: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 12

Program Structure Using Classes

►The presentation tier contains the classes that display information for the user and accept user input

►The business tier contains the logic and calculations that must occur in order to fulfill the requirements of the program

►The persistence tier, sometimes called the data access tier, contains the code required to read and write data from permanent storage

Page 13: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 13

Sample Program Classes

►Presentation tier: The presentation tier contains the RegistrationCostForm class. This class displays the user interface in a Windows Form object and also edits the user input data to ensure its validity

►Business tier: The business tier contains two classes: the Student class and the OnCampusStudent class. The Student class contains data for each registered student and calculates the registration costs for some students. The OnCampusStudent class is used for registered students who live in oncampus residence halls

►Persistence tier: The persistence tier consists of one class, StudentCostsFile, which creates and writes the Student Costs File

Page 14: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 14

Creating a Class

►With Visual Studio open and a Window Application project begun, right-click the project name in the Solution Explorer window and then point to Add on the shortcut menu

►Click Class on the Add submenu►Type Student as the name of the class and then

click the Add button►Using the same techniques, create the

OnCampusStudent class and the StudentCostsFile classes

Page 15: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 15

Creating a Class

Page 16: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 16

Creating a Class

Page 17: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 17

Instantiating a Class and Class Communication

►Whenever you define a class in your Visual Basic program, you must instantiate, or create, an object based on that class in order for the processing within the object to take place

Page 18: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 18

Constructors in New Instantiated Classes

►When a class is instantiated into an object using the New keyword, a special procedure in the instantiated class called a constructor is executed

►The constructor prepares the object for use in the program

Page 19: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 19

Passing Arguments when Instantiating an Object

►Often when instantiating an object, data must be passed to the object

►In the Student class, the New statement must be written with corresponding arguments; that is, the “signature” of the instantiating statement must be the same as the constructor heading in the class

Page 20: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 20

Passing Arguments when Instantiating an Object

Page 21: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 21

Calling a Procedure in a Separate Class

►Most of the time, separate classes in a program contain procedures that must be executed

Page 22: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 22

Inheritance

► Inheritance allows one class to inherit attributes and procedures from another class

Page 23: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 23

Inheritance

Page 24: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 24

Inheritance

Page 25: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 25

Constructors

BASE CLASS CONSTRUCTOR

Page 26: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 26

Constructors

SUBCLASS CONSTRUCTOR

Page 27: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 27

Inheritance and Procedures

►When using inheritance, the subclass can use the procedures within the base class as well as the variables within the base class

►Between the base class and the subclass, five different techniques for referencing and calling a procedure from an outside class such as a Form class can be used

►After the base class and the subclass have been instantiated, the following techniques are available:

Page 28: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 28

Inheritance and Procedures

►Base Class• Call a named procedure in the base class• Call an Overridable procedure in the base

class►Subclass• Call an Overridable Procedure in the subclass• Call a named procedure in the subclass• Call a base class procedure in the subclass

Page 29: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 29

Call a Named Procedure in the Base Class

Page 30: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 30

Calling an Overridable Procedure in a Base Class

Page 31: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 31

Calling an Overridable Procedure in a Subclass

Page 32: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 32

Calling a Named Procedure in the Subclass

Page 33: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 33

Calling a Base Class Procedure in the Subclass

Page 34: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 34

Persistence Classes

►The persistence tier in an application, sometimes called the data access tier, contains classes that are involved in saving and retrieving data that is stored on a permanent storage medium such as a hard disk, a DVD-ROM or a USB drive

Page 35: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 35

Persistence Classes

Page 36: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 36

Comma-Delimited Text File

Page 37: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 37

Comma-Delimited Text File

Page 38: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 38

Program Design

Page 39: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 39

Program Design

Page 40: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 40

Program Design

Page 41: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 41

Event Planning Document

Page 42: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 42

Event Planning Document

Page 43: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 43

Event Planning Document

Page 44: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 44

Event Planning Document

Page 45: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 45

Event Planning Document

Page 46: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 46

Event Planning Document

Page 47: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 47

Summary

►Use the TabIndex Property►Edit input, including MaskedTextBox,TextBox, and

ComboBox objects►Describe the three-tier program structure►Understand a class►Create a class

Page 48: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 48

Summary

►Instantiate an object►Pass arguments when instantiating an object►Write a class constructor►Call a procedure in a separate class

Page 49: CHAPTER ELEVEN

11

Chapter 11: Multiple Classes and Inheritance 49

Summary

►Code a base class and a subclass incorporating inheritance

►Call procedures found in a base class and a subclass

►Write overridable and overrides procedures►Create and write a comma-delimited text file

Page 50: CHAPTER ELEVEN

Microsoft Visual Basic 2008

CHAPTER ELEVEN COMPLETE

Multiple Classesand Inheritance