Download presentation
Presentation is loading. Please wait.
Published byHugh Caldwell Modified over 6 years ago
1
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 10 Creating Classes and Objects
2
Objectives After studying this chapter, you should be able to:
Define a class Instantiate an object from a class that you define Add Property procedures to a class Include data validation in a class Create default and parameterized constructors Microsoft Visual Basic 2005: Reloaded, Second Edition
3
Objectives (continued)
Include methods in a class Overload the methods in a class Create a derived class using inheritance Microsoft Visual Basic 2005: Reloaded, Second Edition
4
Classes and Objects Object-oriented programs are based on objects that are instantiated (created) from classes Properties: attributes that describe the object Methods: behaviors that allow the object to perform tasks A class encapsulates (contains) properties and methods VB.Net has many built-in classes E.g. textbox, form, labels You can define your own classes E.g. Student, Book, DVD An Object is a specific instance of a class E.g. a specific student Microsoft Visual Basic 2005: Reloaded, Second Edition
5
Defining a Class (continued)
Do with SimpleSquare – tryclass3 Microsoft Visual Basic 2005: Reloaded, Second Edition
6
Defining a Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
7
Defining a Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
8
Defining a Class Class statement: defines a class
Use Pascal casing for the class name Define attributes and behaviors of the class within the class Code editor automatically adds the Class statement Microsoft Visual Basic 2005: Reloaded, Second Edition
9
Defining a Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
10
Defining a Class (continued)
In the file using the class Microsoft Visual Basic 2005: Reloaded, Second Edition
11
Example 1 – Using a Class that Contains Public Variables Only
Microsoft Visual Basic 2005: Reloaded, Second Edition
12
Using a Class that Contains Public Variables Only (continued)
Any class variable declared with Public keyword can be accessed by any application that contains an instance of the class Use Pascal case for Public variables in a class Microsoft Visual Basic 2005: Reloaded, Second Edition
13
Using a Class that Contains Public Variables Only (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
14
Using a Class that Contains Public Variables Only (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
15
Using a Class that Contains Public Variables Only (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
16
Using a Class that Contains Public Variables Only (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
17
Using a Class that Contains Public Variables Only (continued)
Access an object’s attributes using the dot operator: objectVariable.attribute Disadvantages of Public variables in a class: The class cannot control the values assigned to the variables Violates the concept of OOP encapsulation Microsoft Visual Basic 2005: Reloaded, Second Edition
18
Example 2 – Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods
Microsoft Visual Basic 2005: Reloaded, Second Edition
19
Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued)
Class variables declared with Private keyword: Can only be used by the class Are hidden from the rest of the application Names should start with underscore _ Application can change class variable values only by using the class’s methods Microsoft Visual Basic 2005: Reloaded, Second Edition
20
Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued)
A Public method in the class for manipulating a class variable Exposes a Private class variable as a Property for use by the application Microsoft Visual Basic 2005: Reloaded, Second Edition
21
Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
22
Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued)
Get block: contains code to allow an application to retrieve the contents of the variable exposed as a Property Set block: allows an application to assign a value to the variable exposed as a Property ReadOnly keyword: makes a property readable but not settable (no Set block) WriteOnly keyword: makes a property settable but not readable (no Get block) Microsoft Visual Basic 2005: Reloaded, Second Edition
23
Constructors Constructor:
A method whose instructions are processed automatically when an object is instantiated from a class Purpose is to initialize the class’s Private variables Method name must be New May or may not have parameters Default constructor: a constructor with no parameters Microsoft Visual Basic 2005: Reloaded, Second Edition
24
Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
25
Methods Other than Constructors
Methods in a class can be Sub or Function procedures Use Pascal casing for method names Microsoft Visual Basic 2005: Reloaded, Second Edition
26
Methods Other than Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
27
Methods Other than Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
28
Methods Other than Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
29
Methods Other than Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
30
Example 3 – Using a Class that Contains Two Constructors
Microsoft Visual Basic 2005: Reloaded, Second Edition
31
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
32
Using a Class that Contains Two Constructors (continued)
Parameterized constructor: a constructor method that contains parameters Method signature: method name and parameter list Best practices: Parameterized constructor should set the values of the class variables using the class’s Property procedures to take advantage of any validation code in the Property procedures Microsoft Visual Basic 2005: Reloaded, Second Edition
33
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
34
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
35
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
36
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
37
Using a Class that Contains Two Constructors (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
38
Example 4 – Using a Class that Contains Overloaded Methods
Microsoft Visual Basic 2005: Reloaded, Second Edition
39
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
40
Using a Class that Contains Overloaded Methods (continued)
Overloaded methods: two or more methods that have the same name but different parameters Constructors can be overloaded To overload a non-constructor method, use the Overloads keyword in the method declaration Many of VB’s built-in methods are overloaded, as shown in the Intellisense feature: Microsoft Visual Basic 2005: Reloaded, Second Edition
41
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
42
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
43
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
44
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
45
Using a Class that Contains Overloaded Methods (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
46
Example 5 – Using a Base Class and a Derived Class
Inheritance: one class can be created from another class Base class: the original class Derived class: the new class created from the base class Inherits keyword: specifies the base class Microsoft Visual Basic 2005: Reloaded, Second Edition
47
Using a Base Class and a Derived Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
48
Using a Base Class and a Derived Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
49
Using a Base Class and a Derived Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
50
Using a Base Class and a Derived Class (continued)
Derived class contains all of the attributes and behaviors of the base class Derived class may also contain its own attributes and behaviors Overridable keyword: indicates the base class method can be overridden by the derived class Overrides keyword: indicates that a method in the derived class overrides the method with the same name in the base class Microsoft Visual Basic 2005: Reloaded, Second Edition
51
Using a Base Class and a Derived Class (continued)
MyBase keyword: refers to the base class MyBase.New: tells the computer to process the code in the base class’s constructor Microsoft Visual Basic 2005: Reloaded, Second Edition
52
Using a Base Class and a Derived Class (continued)
Microsoft Visual Basic 2005: Reloaded, Second Edition
53
Programming Tutorial Microsoft Visual Basic 2005: Reloaded, Second Edition
54
Programming Example Microsoft Visual Basic 2005: Reloaded, Second Edition
55
Summary Objects are instantiated from classes
Classes encapsulate their attributes and behaviors Class Public data members and methods are exposed to any application that creates an object from the class; class Private members are not Class Public properties allow an application to manipulate private class data members ReadOnly keyword allows a property to be retrieved but not changed Microsoft Visual Basic 2005: Reloaded, Second Edition
56
Summary (continued) WriteOnly 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: a sub procedure named New that is processed when an object is created from the class Default constructor: has no parameters Microsoft Visual Basic 2005: Reloaded, Second Edition
57
Summary (continued) Derived class: a new class created from another class (the base class) Overridable keyword: indicates that a method in the base class can be overridden in the derived class Overrides keyword: indicates that a method in the derived class overrides a method of the same name in the base class Inherits clause: creates a derived class MyBase keyword: refers to the base class Microsoft Visual Basic 2005: Reloaded, Second Edition
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.