Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET."— Presentation transcript:

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

2 © 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

3 © 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

4 © 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)

5 © 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

6 © 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

7 © 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

8 © 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

9 © 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

10 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 10 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

11 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 11 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

12 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 12 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 …)

13 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 13 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

14 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 14 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

15 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 15 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

16 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 16 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

17 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 17 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

18 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 18 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

19 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 19 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

20 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 20 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

21 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 21 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

22 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 22 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))

23 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 23 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

24 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 24 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

25 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 25 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

26 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 26 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

27 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 27 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

28 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 28 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

29 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 29 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

30 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved. 6 30 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)


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

Similar presentations


Ads by Google