creating classes and objects chapter microsoft visual basic.net: reloaded 1

44
Creating Classes and Objects Chapter Microsoft Visual Basic .NET: Reloaded 1

Upload: patience-atkinson

Post on 03-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

Creating Classes and Objects

Chapter Microsoft Visual Basic .NET: Reloaded

1

Page 2: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

2Microsoft Visual Basic .NET: Reloaded

Objectives

• Define a class

• Add properties to a class

• Instantiate an object from a class that you define

• Add Property procedures to a class

Page 3: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

3Microsoft Visual Basic .NET: Reloaded

Objectives (continued)

• Create constructors

• Add methods to a class

• Include data validation in a class

Page 4: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

4Microsoft Visual Basic .NET: Reloaded

Classes and Objects

• Object-oriented programs are based on objects, which are instantiated from classes

• A class contains (encapsulates) the properties (attributes) that describe a person, place, or thing

• Examples: Student, School, Book

• An object is a specific instance of a class

• Examples: “You” are a student that goes to a specific school and are using this specific book

• VB has many built-in objects such as Textbox controls, Forms, and Labels

Page 5: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

5Microsoft Visual Basic .NET: Reloaded

Defining a Class• Use the Class statement

• Begins with keyword Class and ends with End Class

Page 6: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

6Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 7: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

7Microsoft Visual Basic .NET: Reloaded

Defining a Class (continued)

Page 8: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

8Microsoft Visual Basic .NET: Reloaded

Defining a Class (continued)

Page 9: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

9Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 10: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

10Microsoft Visual Basic .NET: Reloaded

Example 1 – Using a Class that Contains Properties Only

• Assume that the sales manager at Sweets Unlimited wants an application that allows him to save each salesperson’s

• name

• quarterly sales amount

• quarterly bonus amount in a sequential access file

• The bonus amount is calculated by multiplying the sales amount by 5%

Page 11: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

11Microsoft Visual Basic .NET: Reloaded

Example 1 – Using a Class that Contains Properties Only (continued)

• Figure 11.6 shows a sample run of the Sweets Unlimited application

Page 12: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

12Microsoft Visual Basic .NET: Reloaded

Example 1 – Using a Class that Contains Properties Only (continued)

• Figure 11.7 shows the Salesperson class defined in the Salesperson.vb file

Page 13: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

13Microsoft Visual Basic .NET: Reloaded

Example 1 – Using a Class that Contains Properties Only (continued)

Page 14: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

14Microsoft Visual Basic .NET: Reloaded

Example 1 – Using a Class that Contains Properties Only (continued)

Page 15: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

15Microsoft Visual Basic .NET: Reloaded

Example 2 – Using a Class that Contains Properties and Methods

• Create a class named Square and then use in the Area application

• Square class creates an object that can calculate and return the area of a square, using side measurement provided by application

• Figure 11.9 shows a sample run of the Area application

• Figure 11.10 shows the Square class defined in the Square.vb file

Page 16: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

16Microsoft Visual Basic .NET: Reloaded

Example 2 – Using a Class that Contains Properties and Methods

(continued)

Page 17: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

17Microsoft Visual Basic .NET: Reloaded

Example 2 – Using a Class that Contains Properties and Methods

(continued)

Page 18: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

18Microsoft Visual Basic .NET: Reloaded

Example 2 – Using a Class that Contains Properties and Methods

(continued)

• Creating a Public Property

• Declare a class-level private variable to hold the value to be stored by the property

• Data type of property and private variable must match each other

• Heading begins with keywords Public Property

Page 19: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

19Microsoft Visual Basic .NET: Reloaded

Example 2 – Using a Class that Contains Properties and Methods

(continued)

• Code Get block to retrieve contents of private variable

• Code Set block to change contents of private variable

• Ends with keywords End Property

Page 20: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

20Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 21: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

21Microsoft Visual Basic .NET: Reloaded

Constructors

• Constructor method whose instructions the computer processes, automatically, each time an object is created (instantiated) from the class

• Constructor begins with Public Sub New followed by a set of optional parameters

• Parameter list may be empty - New()

• No parameters is called “default constructor”

• Every class should have at least one constructor and may have several

Page 22: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

22Microsoft Visual Basic .NET: Reloaded

HOW TO…

Page 23: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

23Microsoft Visual Basic .NET: Reloaded

Methods Other Than Constructors

• Sub methods do not return a value• Function methods return a value to the

calling procedure

Page 24: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

24Microsoft Visual Basic .NET: Reloaded

Methods Other Than Constructors (continued)

Page 25: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

25Microsoft Visual Basic .NET: Reloaded

Example 3 – Using a Class that contains two Constructors and Data

Validation

• Create a class named MyDate and then use in the Personnel application

• MyDate class creates an object that returns a month number, followed by a slash, and a day number

• Figure 11.15 shows a sample run of the Personnel application

• Figure 11.16 shows the MyDate class defined in the MyDate.vb file

Page 26: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

26Microsoft Visual Basic .NET: Reloaded

Example 3 – Using a Class that contains two Constructors and Data

Validation (continued)

Page 27: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

27Microsoft Visual Basic .NET: Reloaded

Example 3 – Using a Class that contains two Constructors and Data

Validation (continued)

Page 28: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

28Microsoft Visual Basic .NET: Reloaded

Example 3 – Using a Class that contains two Constructors and Data

Validation (continued)

Page 29: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

29Microsoft Visual Basic .NET: Reloaded

Example 3 – Using a Class that contains two Constructors and Data

Validation (continued)

Page 30: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

30Microsoft Visual Basic .NET: Reloaded

Programming Example – Kessler Landscaping Application

• Monica Kessler, the owner of Kessler Landscaping, wants an application that she can use to estimate the cost of laying sod

• Use a MyRectangle class in this application

Page 31: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

31Microsoft Visual Basic .NET: Reloaded

TOE Chart

Page 32: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

32Microsoft Visual Basic .NET: Reloaded

User Interface

Page 33: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

33Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings

Page 34: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

34Microsoft Visual Basic .NET: Reloaded

Tab Order

Page 35: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

35Microsoft Visual Basic .NET: Reloaded

Pseudocode

btnExit Click event procedure1. close application

btnCalc Click event procedure1. declare a MyRectangle object2. assign the length and width to the MyRectangle object’s properties3. assign the sod price to a variable4. calculate the area of the rectangle5. calculate the total price of the sod6. display the total price of the sod in lblTotalPrice

txtLength, txtWidth, and txtPrice TextChanged event procedures1. clear the contents of the lblTotalPrice control

Page 36: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

36Microsoft Visual Basic .NET: Reloaded

Code (MyRectangle.vb file)

Page 37: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

37Microsoft Visual Basic .NET: Reloaded

Code (MyRectangle.vb file) (continued)

Page 38: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

38Microsoft Visual Basic .NET: Reloaded

Code (Kessler Form.vb file)

Page 39: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

39Microsoft Visual Basic .NET: Reloaded

Code (Kessler Form.vb file) (continued)

Page 40: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

40Microsoft Visual Basic .NET: Reloaded

Summary

• The objects used in an object-oriented program are created, or instantiated, from classes

• A class contains (encapsulates) the properties (attributes) that describe the object it creates, and the methods (behaviors) that allow the object to perform tasks

• In Visual Basic .NET, you can create objects from classes that you define with the Class statement

Page 41: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

41Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Good programming practice to enter Option Explicit On and Option Strict On statement in both the form file and class file

• The first letter in the class name, as well as the first letter in any subsequent words in the name, should be capitalized

• The properties in a class should be assigned a name composed of one or more words, with the first letter of each word being capitalized

• You should use nouns and adjectives to name a property

Page 42: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

42Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• Methods in a class should be assigned a name composed of one or more words, with the first letter of each word being capitalized

• You should use a verb for the first word in the name, and nouns and adjectives for any subsequent words in the name

• Variables declared using the Public keyword in a class definition can be accessed by any application that uses an object created from the class

• Most classes contain properties and methods

Page 43: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

43Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• When an application needs to assign data to or retrieve data from a Private variable in a class, it must use a Public property to do so• You create a Public property using a Property

procedure

• The Get block in a Property procedure allows an application to access the contents of the class’s Private variables

• The Set block in a Property procedure allows an application to assign values to the class’s Private variables

Page 44: Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1

44Microsoft Visual Basic .NET: Reloaded

Summary (continued)

• A class can have one or more constructors

• All constructions are Sub procedures

• The default constructor is automatically processed when an object is created from the class