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

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic th Edition
1.
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
1.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 8: String Manipulation
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Chapter Four The Selection Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Chapter 6: The Repetition Structure
Chapter Two Creating a First Project in Visual Basic.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
1.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Chapter 1: An Introduction to Visual Basic 2015
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development Programming with Visual Basic
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

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

2 Objectives Define a class Add properties to a class Instantiate an object from a class that you define Add Property procedures to a class

3 Microsoft Visual Basic.NET: Reloaded Objectives ( continued ) Create constructors Add methods to a class Include data validation in a class

4 Microsoft 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

5 Microsoft Visual Basic.NET: Reloaded Defining a Class Use the Class statement Begins with keyword Class and ends with End Class

6 Microsoft Visual Basic.NET: Reloaded HOW TO…

7 Microsoft Visual Basic.NET: Reloaded Defining a Class ( continued )

8 Microsoft Visual Basic.NET: Reloaded Defining a Class ( continued )

9 Microsoft Visual Basic.NET: Reloaded HOW TO…

10 Microsoft 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%

11 Microsoft 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

12 Microsoft 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

13 Microsoft Visual Basic.NET: Reloaded Example 1 – Using a Class that Contains Properties Only ( continued )

14 Microsoft Visual Basic.NET: Reloaded Example 1 – Using a Class that Contains Properties Only ( continued )

15 Microsoft 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 shows the Square class defined in the Square.vb file

16 Microsoft Visual Basic.NET: Reloaded Example 2 – Using a Class that Contains Properties and Methods ( continued )

17 Microsoft Visual Basic.NET: Reloaded Example 2 – Using a Class that Contains Properties and Methods ( continued )

18 Microsoft 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

19 Microsoft 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

20 Microsoft Visual Basic.NET: Reloaded HOW TO…

21 Microsoft 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

22 Microsoft Visual Basic.NET: Reloaded HOW TO…

23 Microsoft Visual Basic.NET: Reloaded Methods Other Than Constructors Sub methods do not return a value Function methods return a value to the calling procedure

24 Microsoft Visual Basic.NET: Reloaded Methods Other Than Constructors ( continued )

25 Microsoft 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 shows a sample run of the Personnel application Figure shows the MyDate class defined in the MyDate.vb file

26 Microsoft Visual Basic.NET: Reloaded Example 3 – Using a Class that contains two Constructors and Data Validation ( continued )

27 Microsoft Visual Basic.NET: Reloaded Example 3 – Using a Class that contains two Constructors and Data Validation ( continued )

28 Microsoft Visual Basic.NET: Reloaded Example 3 – Using a Class that contains two Constructors and Data Validation ( continued )

29 Microsoft Visual Basic.NET: Reloaded Example 3 – Using a Class that contains two Constructors and Data Validation ( continued )

30 Microsoft 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

31 Microsoft Visual Basic.NET: Reloaded TOE Chart

32 Microsoft Visual Basic.NET: Reloaded User Interface

33 Microsoft Visual Basic.NET: Reloaded Objects, Properties, and Settings

34 Microsoft Visual Basic.NET: Reloaded Tab Order

35 Microsoft Visual Basic.NET: Reloaded Pseudocode btnExit Click event procedure 1. close application btnCalc Click event procedure 1. declare a MyRectangle object 2. assign the length and width to the MyRectangle object’s properties 3. assign the sod price to a variable 4. calculate the area of the rectangle 5. calculate the total price of the sod 6. display the total price of the sod in lblTotalPrice txtLength, txtWidth, and txtPrice TextChanged event procedures 1. clear the contents of the lblTotalPrice control

36 Microsoft Visual Basic.NET: Reloaded Code (MyRectangle.vb file)

37 Microsoft Visual Basic.NET: Reloaded Code (MyRectangle.vb file) ( continued )

38 Microsoft Visual Basic.NET: Reloaded Code (Kessler Form.vb file)

39 Microsoft Visual Basic.NET: Reloaded Code (Kessler Form.vb file) ( continued )

40 Microsoft 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

41 Microsoft 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

42 Microsoft 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

43 Microsoft 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

44 Microsoft 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