Microsoft Visual Basic 2010 Reloaded Fourth Edition Chapter
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Fourteen Creating Classes and Objects
Objectives After studying this chapter, you should be able to: • Explain the terminology used in object-oriented programming • Create a class • Instantiate an object • Add Property procedures to a class • Include data validation in a class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 2
Objectives (cont'd. ) • • Create default and parameterized constructors Include methods other than constructors in a class Overload the methods in a class Create an auto-implemented property Microsoft Visual Basic 2010: Reloaded, Fourth Edition 3
Object-Oriented Programming Terminology • Object-oriented programming (OOP) language: allows the programmer to use objects to accomplish goals • Object: anything that can be seen, touched, or used • Objects are instantiated (created) from a class • Each object is referred to as an instance of the class • VB. Net has many built-in classes, including its GUI controls Microsoft Visual Basic 2010: Reloaded, Fourth Edition 4
Object-Oriented Programming Terminology (cont'd. ) • Attributes: characteristics that describe the object • An object’s behaviors include methods and events • Methods: operations that the object is capable of performing • Events: actions to which an object can respond • A class encapsulates (contains) all of the attributes and behaviors of the object it instantiates Microsoft Visual Basic 2010: Reloaded, Fourth Edition 5
Creating a Class • Can define your own classes and create instances (objects) from them – Must specify the attributes and behaviors of the objects created from the class • Class statement: defines a class • Use Pascal casing for the class name • Define attributes and behaviors of the class within it – Attributes represented by variables and Property procedures Microsoft Visual Basic 2010: Reloaded, Fourth Edition 6
Creating a Class (cont'd. ) Figure 14 -1: How to define a class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 7
Creating a Class (cont'd. ) Figure 14 -2: Class statement entered in a class file named Time. Card. vb Microsoft Visual Basic 2010: Reloaded, Fourth Edition 8
Creating a Class (cont’d. ) • After you define a class, it can be used to instantiate one or more objects • Use the New keyword to instantiate an object Microsoft Visual Basic 2010: Reloaded, Fourth Edition 9
Figure 14 -3: How to instantiate an object from a class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 10
Example 1 – A Class that Contains Public Variables Only • Willow Pools application Figure 14 -4: Sample run of the Willow Pools application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 11
Figure 14 -5: Code for the Willow Pools application (using a class) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 12
Example 1 (cont’d. ) Figure 14 -5: Code for the Willow Pools application (using a class) (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 13
Example 1 (cont’d. ) • Class statement is entered in a class file • Each attribute is represented by a Public variable • Any class variable declared with Public keyword can be accessed by any application that contains an instance of the class – In OOP terminology, the variables are exposed to the application • Use Pascal case for Public variables in a class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 14
Private Variables and Public Property Procedures • Disadvantages of Public variables in a class: – Class cannot control values assigned to the variables – Violates the concept of OOP encapsulation • Variables are rarely declared as Public in a class • Private variables in a class are not visible to applications containing an instance of the class • A Public property can be used to assign data to or retrieve data from a Private variable in a class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 15
Private Variables and Public Property Procedures (cont’d. ) • Only Public members of an object’s class are visible to an application • Property procedure: used to create a Public property • Public Property keywords are used in the property header – Can optionally specify Read. Only or Write. Only • Must specify the data type of the property in the property header Microsoft Visual Basic 2010: Reloaded, Fourth Edition 16
Private Variables and Public Property Procedures (cont’d. ) • Get block: contains code to allow application to retrieve contents of the variable associated with the Property • Set block: allows application to assign a value to the variable associated with the Property • Read. Only keyword: makes a property readable but not settable (no Set block) • Write. Only keyword: makes a property settable but not readable (no Get block) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 17
Private Variables and Public Property Procedures (cont’d. ) Figure 14 -6: How to create a property procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 18
Figure 14 -6: How to create a property procedure (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 19
Private Variables and Public Property Procedures (cont’d. ) • Get statement: creates a Get block – Starts with Get, ends with End Get – Usually contains only Return private. Variable • Set statement: creates a Set block – Starts with Set, ends with End Set – Has a parameter value with a data type that matches the data type of the class variable associated with the Property Microsoft Visual Basic 2010: Reloaded, Fourth Edition 20
Constructors • Constructor: – Class method whose purpose is to initialize the class’s Private variables – Always named New – May or may not have parameters – Never returns a value • Default constructor: constructor with no parameters – Can only be one default constructor per class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 21
Constructors (cont’d. ) • Parameterized constructor: a constructor that contains one or more parameters • A class can have as many parameterized constructors as needed, but each one must have a unique parameter list • Method signature: the method name combined with its optional parameter list • Computer determines which constructor to use by matching the number, type, and position of the parameters in the parameter list Microsoft Visual Basic 2010: Reloaded, Fourth Edition 22
Constructors (cont'd. ) Figure 14 -7: How to create a constructor Microsoft Visual Basic 2010: Reloaded, Fourth Edition 23
Methods Other than Constructors • Methods in a class can be Sub or Function procedures • Use Pascal casing for method names – First word should be a verb – Following words should be nouns and adjectives Microsoft Visual Basic 2010: Reloaded, Fourth Edition 24
Methods Other than Constructors (cont'd. ) Figure 14 -8: How to create a method that is not a constructor Microsoft Visual Basic 2010: Reloaded, Fourth Edition 25
Example 2 – A Class that Contains Private Variable, Public Properties, and Methods • Carpet Haven application: calculates and displays number of sq. yds. of carpeting and cost of the carpet Figure 14 -9: Sample run of the Carpet Haven application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 26
Example 2 (cont'd. ) Figure 14 -10: Partial code for the Carpet Haven application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 27
Figure 14 -10: Partial code for the Carpet Haven application (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 28
Figure 14 -10: Partial code for the Carpet Haven application (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 29
Figure 14 -11: A different way of coding the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 30
Example 3 – Reusing a Class • Can use the Rectangle class to represent a square pizza • A square is a rectangle with four equal sides • The ability to reuse an object saves programming time and money Microsoft Visual Basic 2010: Reloaded, Fourth Edition 31
Example 3 (cont’d. ) Figure 14 -12: Sample run of the Pizza Roma application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 32
Figure 14 -13: Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 33
Example 4 – A Class that Contains Overloaded Methods • Employee class: two attributes – Employee number and employee name • Four behaviors: – Can initialize its attributes using values provided by the class – Can initialize its attributes using values provided by the application – Can calculate and return gross pay for salaried employees – Can calculate and return gross pay for hourly employees Microsoft Visual Basic 2010: Reloaded, Fourth Edition 34
Example 4 – (cont’d. ) • Employee class has two constructors – Default constructor with no parameters – Parameterized constructor • Overloaded methods: two or more methods with the same name but different parameters • Can overload constructors and any methods in a class • Intellisense feature displays the method signatures Microsoft Visual Basic 2010: Reloaded, Fourth Edition 35
Figure 14 -14: Employee class definition Microsoft Visual Basic 2010: Reloaded, Fourth Edition 36
Example 4 – (cont’d. ) Figure 14 -14: Employee class definition (cont'd. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 37
Example 4 – A Class that Contains Overloaded Methods (cont’d. ) Figure 14 -15: First of the To. String method’s signatures Microsoft Visual Basic 2010: Reloaded, Fourth Edition 38
Example 4 – A Class that Contains Overloaded Methods (cont’d. ) Figure 14 -16: Sample run of the ABC Company application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 39
Figure 14 -16: Code entered in the Calculate button’s Click event procedure Microsoft Visual Basic 2010: Reloaded, Fourth Edition 40
Auto-Implemented Properties • Auto-implemented properties: enable you to specify the property of a class in one line of code • VB automatically creates a hidden private variable that it associates with the property – It creates hidden Get and Set blocks – Private variable’s name is the same as the Property name but is preceded by an underscore • You will need to use the standard syntax if you need to add validation code to the Set block or if you want the property to be Read. Only or Write. Only Microsoft Visual Basic 2010: Reloaded, Fourth Edition 41
Auto-Implemented Properties (cont'd. ) Figure 14 -18: How to create an auto-implemented property Microsoft Visual Basic 2010: Reloaded, Fourth Edition 42
Auto-Implemented Properties (cont'd. ) Figure 14 -19: Employee class definition using auto-implemented properties Microsoft Visual Basic 2010: Reloaded, Fourth Edition 43
Auto-Implemented Properties (cont'd. ) Figure 14 -19: Employee class definition using auto-implemented properties (cont’d. ) Microsoft Visual Basic 2010: Reloaded, Fourth Edition 44
Programming Tutorial 1 • Creating the Grade Calculator application Figure 14 -22: Main. Form for the Grade Calculator application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 45
Programming Tutorial 2 • Modifying the Roll ’Em Game application Figure 14 -25: Sample run of the modified Roll ‘Em Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 46
Programming Example • Kessler Landscaping application Figure 14 -28: Main. Form in the Kessler Landscaping application Microsoft Visual Basic 2010: Reloaded, Fourth Edition 47
Summary • • Objects are instantiated from classes Classes encapsulate their attributes and behaviors Class statement: used to define a class Class Public data variables cannot be controlled by the class • Most classes contain properties and methods • Public members of a class are exposed to the application, while Private members are hidden • Application must use a Public property to assign data to or retrieve data from a Private class variable Microsoft Visual Basic 2010: Reloaded, Fourth Edition 48
Summary (cont'd. ) • Read. Only allows a property to be retrieved but not changed • Write. Only allows a property to be changed but not retrieved • Property Get block implements retrieving the property value • Property Set block implements changing the property value • Constructor: sub procedure named New that is processed when an object is created from the class Microsoft Visual Basic 2010: Reloaded, Fourth Edition 49
Summary (cont'd. ) • Default constructor: constructor with no parameters • Parameterized constructor: a constructor with parameters • A class may have one or more constructors • Computer processes the constructor whose parameters match in number, data type, and position • You can overload methods in a class • VB creates the Private variable and its Get and Set blocks for an auto-implemented property Microsoft Visual Basic 2010: Reloaded, Fourth Edition 50
- Slides: 50