Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition.

Similar presentations


Presentation on theme: "Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition."— Presentation transcript:

1 Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition

2 2 Classes and Objects Lesson A Objectives Define a class Add properties to a class Instantiate an object from a class that you define

3 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

4 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

5 Programming with Microsoft Visual Basic.NET, Second Edition 5 Defining a Class (continued) Figure 11-3: Syntax and an example of the Class statement

6 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

7 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

8 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%

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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)

17 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

18 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

19 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

20 Programming with Microsoft Visual Basic.NET, Second Edition 20 Constructors (continued) Figure 11-14: Syntax and an example of creating a constructor

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 Programming with Microsoft Visual Basic.NET, Second Edition 29 Coding the Cornwall Calendars Application (continued) Figure 11-21: Interface for the Cornwall Calendars application

30 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

31 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)

32 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

33 Programming with Microsoft Visual Basic.NET, Second Edition 33 Creating the Check Class (continued) Figure 11-23: Pseudocode for the Check class

34 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

35 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

36 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

37 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

38 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

39 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


Download ppt "Chapter 11: Classes and Objects Programming with Microsoft Visual Basic.NET, Second Edition."

Similar presentations


Ads by Google