Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.

Slides:



Advertisements
Similar presentations
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
OOP-Creating Object-Oriented Programs
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Advanced Object-Oriented Programming Features
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
A First Program Using C#
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter 8: Writing Graphical User Interfaces
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Teacher: Ms. Olifer MICROSOFT VISUAL STUDIO 2010: PROPERTIES OF WINDOWS FORM OBJECT.
Chapter 9: Getting Comfortable with Object- Oriented Programming.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JavaScript, Fourth Edition
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
CHAPTER THREE Representing Data: Constants and Variables.
Object-Oriented Programming: Inheritance and Polymorphism.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object-Oriented Programming: Classes and Objects.
Multiple Classes and Inheritance
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 11 – Object-Oriented Programming
Programming with Microsoft Visual Basic 2008 Fourth Edition
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 2 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object –Text, ForeColor, Checked, Visible, Enabled Methods - like a verb, an action or behavior, something the object can do or have done to it –ShowDialog, Focus, Clear, ToUpper, ToLower Events - object response to user action or other events –Click, Enter, Activate

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 3 Thus Far... Since Chapter 1 we have been using objects Up until now the classes for all objects used have been predefined We have created new objects for these classes by using the controls in the Toolbox VB allows programmers to create their own object types by creating a Class Module

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 4 Class and Instance When we add a button from the the toolbox to the form we are creating an Instance of the Button Class The button object (control) on a form is an Instance of the Button Class Another way to instantiate class is using New key word Defining your own Class is like creating a new tool for the Toolbox Dim fntMyFont = New Font ("Arial", 12) OR lblMsg.Font = fntMyFont lblMsg.Font = New Font ("Arial", 12)

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 5 Encapsulation Combination of characteristics of an object along with its behavior in "one package“ Sometimes referred to as data hiding; an object expose only some properties and procedures Cannot make object do anything it doesn't already "know" how to do Cannot make up new properties, methods, or events

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 Inheritance Ability to create a new class from an existing class Purpose of Inheritance is reusability –Original class is called Base Class, Superclass, or Parent Class –Inherited class is called Subclass, Derived Class, or Child Class

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 7 Inheritance: Example 1 For example, each form created is inherited from the existing Form class Examine 1st line of code for a form in the Editor Public Class Form1 Inherits System.Windows.Forms.Form Inherited Class, Derived Class Subclass, Child Class Base Class, Superclass, Parent Class

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 8 Inheritance: Example 2 Base Class –Person Subclasses –Employee –Customer –Student Person -Name -Address -Phone EmployeeStudentCustomer

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 9 Purpose: Code Reusability The main purpose behind OOP and, especially, inheritance New classes created with Class Module can be used in multiple projects Each object created from the class can have its own properties

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Purpose Code Separation: Multitier Applications Common use of classes is to create multitier applications Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines Goal is to create components that can be combined, replaced, and modified independently

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Three-tier Model Most common implementation of multitier Presentation TierBusiness TierData Tier User Interface Forms Controls Menus Business Objects Validation Calculations Business Logic Business Rates Data Retrieval Data Storage

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Create a new class Design a new class (e.g. BookSale) –Determine its Properties (e.g. Title, Quantity, Price) –Determine Methods (Actions) (e.g. ExtendedPrice ) Create a new class –Select: Project, Add Class…, Class –Name the Class (e.g. “BookSale”) –Do it (pp. 247 …)

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Declare Class Properties Define variables inside the Class Module by declaring them as Private Do not make Public - that would violate Encapsulation principle (each object should be in charge of its own data) Do: Private mintPatientNum as Integer Private mdtmDate as Date Private mstrLastName as String

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Define Values with Property Procedures Return property Values from the class module with Get Pass property Values to the class module with Set Private ClassVariable As DataType [Public] Property PropertyName As DataType ‘Property name for outside Get‘ Retrieves (reads) property value from a class PropertyName = ClassVariable ‘ End Get Set (ByVal Value As DataType) ‘A ssigns (writes) value to property [statements, such as validation] ClassVariable = Value End Set End Property

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Read-Only Properties In some cases a value for a property should only be retrieved by an object and not changed Create ReadOnly property with only Get clause ReadOnly Property Subtotal() As Decimal Get Subtotal = mdecSubtotal End Get End Property

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Create Class Methods Create methods by coding Public Sub and Function procedures Public Function ExtendedPrice() As Decimal Return mintQuantity * mdecPrice End Function Public Sub CalculateTotals() Dim decSaleAmount As Decimal decSaleAmount = mdecSalePrice + mdecPriceOfExtras mdecSaleTax = decSaleAmount * mdecTAX_RATE mdecSubtotal = decSaleAmount + mdecSaleTax mdecAmount = mdecSubtotal - mdecAllowance End Sub

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Create Regions Regions of code allow sections of code to be hidden in the same way that the Editor hides Windows generated code To add a region –Include #Region Statement followed by a string literal giving the region's name –#End Region tag will be added automatically –Include code between the 2 statements

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Instantiating and Using Objects Classes are not objects but definitions of objects You can use objects as instances of a class In a Form create and instance of a Class –Declare an object variable with datatype of the class –Then, instantiate the object using the New keyword Good Practice of Instantiating the object –Only when(if) it is needed –Inside a Try/Catch block for error handling (The block must be inside a procedure) Use objects just as another object: –ObjectName.PropertyName –ObjectName.MethodName

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Instance versus Shared Variables Instance variables or properties –Separate memory location for each instance of the object Shared variables or properties –Single memory location that is available for ALL objects of a class –Can be accessed without instantiating an object of the class –Use the Shared keyword to create variables and write corresponding Property procedures Used for accumulating sums and counts (pp.255…) Shared Methods can also be created

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Polymorphism and Overloading Polymorphism: Different classes of objects may have different methods with the same name, e.g., Radio Buttons, Check boxes, and List boxes have Select method. Overloading is a way to implement polymorphism by calling the same method with different signature (argument list), e.g. MessageBox.Show

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Constructors A method that automatically executes when an object is instantiated Ideal for initialization tasks, e.g. opening a DB connection Create as Public Sub New empty and overloaded constructor procedures : Sub New() 'Constructor with empty argument list (created automatically) End Sub Sub New(ByVal Title As String, ByVal Quantity As Integer, _ ByVal Price As Decimal) Me.Title = Title Me.Quantity = Quantity Me.Price = Price End Sub

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Parameterizing Constructor Passing initial property values to constructor when creating an object Instead of You instantiate an object passing the values mBookSale = New BookSale() 'instantiate the object 'Input user entries. mBookSale.Title = txtTitle.Text mBookSale.Quantity = CInt(txtQuantity.Text) mBookSale.Price = CDec(txtPrice.Text) mBookSale = New BookSale(txtTitle.Text, _ CInt(txtQuantity.Text), CDec(txtPrice.Text))

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Destructors Destructor –Method that automatically executes when an object is destroyed –Create by writing a Finalize procedure Garbage Collection is a feature of.NET Common Language Runtime (CLR) CLR periodically checks for unreferenced objects and releases memory and system resources used by the objects Microsoft recommends depending on Garbage Collection rather than using Finalize procedures

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Inheritance Implemented Ability to create a new class from an existing one New class can –Be based on another class (base class) –Inherit the properties and methods (but not constructors!) of the base class, which can be One of the VB existing classes Your own class Designate inheritance by adding the Inherits statement referencing the base class with the name space

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Specifying a Namespace In your projects, you have noticed the Inherits clause when VB creates a new form class Entire namespace is not needed for classes in the namespaces that are automatically included in a Windows Forms project –System; System.Windows.Forms; System.Drawing Public Class Form1 Inherits System.Windows.Forms.Form Class Name Namespace Public Class StudentBookSale Inherits BookSale

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Inheriting Variables and Methods In the Base class, substitute Protected for Private modifiers to permit inheritance You can call base class constructor from the derived class constructor, using MyBase.New statement Sub New(ByVal Title As String, ByVal Quantity As Integer, _ ByVal Price As Decimal) MyBase.New() Me.Title = Title Me.Quantity = Quantity Me.Price = Price End Sub ‘Instead of: Private mstrTitle as String Protected mstrTitle as String

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Polymorphism and Overriding Methods created in subclass may have the same name and the same signature as in the base class Overriding is a way of implementing polymorphism by method in subclass taking precedence To override a base class method –Declare the base class method with the Overridable keyword: –Declare the subclass method with the Overrides keyword Overridable Function ExtendedPrice() as Decimal Overrides Function ExtendedPrice() as Decimal

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Inheriting Form Classes Many projects require several forms Create a base form and inherit the visual interface to new forms Base form inherits from System.Windows.Forms.Form Subclass from inherits from Base form

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Creating Inherited Form Class Project menu, Add Windows Form Modify the Inherits Statement to inherit from base form using project name as the namespace OR Project menu, Add Inherited Form In dialog select name of Base form

© 2001 by The McGraw-Hill Companies, Inc. All rights reserved Referencing Values on a Different Form Use the identifier for the other form's instance to refer to controls on different form General Syntax Example FormInstance.ControlName.Property Dim frmSumInstance as New frmSum( ) frmSumInstance.lblTotal.Text=FormatCurrency(mdecTotal)