Chapter 6 OOP Creating ObjectOriented Programs Programming In
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic. NET © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object Terminology Review • Object - like a noun, a thing – Buttons, Text Boxes, Labels • Properties - like an adjective, characteristics of object – Text, Fore. Color, Checked, Visible, Enabled • Methods - like a verb, an action or behavior, something the object can do or have done to it – Show. Dialog, Focus, Clear, To. Upper, To. Lower • Events - object response to user action or other events – Click, Enter, Activate 2 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
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 3 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Class and Instance • When we add a button object from the button tool in the toolbox to the form we are creating an Instance of the Button Class • The button object is an Instance of the Button Class • Every button on the form is an Instance • Defining your own Class is like creating a new tool for the Toolbox 4 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
"Cookie Analogy" • Class = Cookie cutter • Instantiate = Making a cookie using the cookie cutter • Instance = Newly made cookie • Properties of the Instance may have different values – Icing property can be True or False – Flavor property could be Lemon or Chocolate 5 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
"Cookie Analogy" (cont. ) • Methods = Eat, Bake, or Crumble • Events = Cookie crumbling all by itself and informing you Methods and Events are often difficult to distinguish! 6 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Encapsulation • Combination of characteristics of an object along with its behavior in "one package" • Cannot make object do anything it doesn't already "know" how to do • Cannot make up new properties, methods, or events • Sometimes referred to as data hiding; an object can expose only those data elements and procedures that it wishes 7 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance • Ability to create a new class from an existing class • Purpose of Inheritance is reusability • For example, each form created is inherited from the existing Form class – Original class is called Base Class, Superclass, or Parent Class – Inherited class is called Subclass, Derived Class, or Child Class 8 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance (cont. ) • Examine 1 st line of code for a form in the Editor Inherited Class, Derived Class Subclass, Child Class Public Class Form 1 Inherits System. Windows. Form Base Class, Superclass, Parent Class 9 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance Example • Base Class Person – Person -Name -Address -Phone • Subclasses – Employee – Customer – Student 10 Employee Customer Student © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Polymorphism • Different classes of objects may have behaviors that are named the same but are implemented differently • Programmers can request an action without knowing exactly what kind of object they have or exactly how it will carry out the action 11 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Polymorphism Implemented • Overloading – Argument type determines which version of a method is used – Example: Message. Box. Show method • Overriding – Refers to a class that has the same method name as its base class – Method in subclass takes precedence 12 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Reusability • The main purpose behind OOOP and Inheritance in particular • New classes created with Class Module can be used in multiple projects • Each object created from the class can have its own properties 13 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
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 and replaced 14 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Three-tier Model • Most common implementation of multitier Presentation Tier Business Tier User Interface Forms Controls Menus Business Objects Validation Calculations Business Logic Business Rates 15 Data Tier Data Retrieval Data Storage © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Instantiating An Object • Creating a new object based on a class • Create an instance of the class by using the New keyword and specify the class • General Form New class. Name ( ) 16 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Examples of Instantiating An Object Dim fnt. My. Font = New Font ("Arial", 12) lbl. Msg. Font = fnt. My. Font OR lbl. Msg. Font = New Font ("Arial", 12) 17 © 2001 by The Mc. Graw-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 Name of the Class Public Class Form 1 Inherits System. Windows. Form Namespace 18 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Namespace (cont. ) • Entire namespace is not needed for any classes in the namespaces that are automatically included in a Windows Forms project which include – System. Windows. Forms – System. Drawing • When referring to classes in a different namespace – Write out the entire namespace – Add and Imports Statement to include the namespace 19 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Class Design - Analyze: • Characteristics of your new objects – Characteristics will be properties – Define the properties as variables in the class module • Behaviors of your new objects – Behaviors will be methods – Define the methods as sub procedures and functions in the class module 20 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Create a New Class • • • 21 Project, Add Class Module Add New Item dialog, choose Class Name the Class Define the Class properties Code the methods © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Properties of a Class • Define variables inside the Class Module by declaring them as Private • Do not make Public-that would violate Encapsulation (each object should be in charge of its own data) • Private mint. Patient. Num as Integer • Private mdtm. Date as Date • Private mstr. Last. Name as String 22 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Assign Values to Properties • Write special property procedures to – Pass the values to the class module – Return values from the class module • Name used for property procedure is the name of the property seen by the outside world • Property Get – Retrieves property values from a class – Like a function must return a value • Property Set – Sets or assigns values to properties 23 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Property Procedure General Form Private Class. Variable As Data. Type [Public] Property. Name As Data. Type Get Property. Name = Class. Variable End Get Set (By. Value As Data. Type) [statements, such as validation] Class. Variable = Value End Set End Property 24 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Read-Only Properties • In some instances a value for a property should only be retrieved by an object and not changed – Create a read-only property by using the Read. Only modifier – Write only a Get portion of the property procedure [Public] Read. Only Property. Name As Data. Type 25 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Code a Method • Create methods by adding sub procedures and functions for the behaviors to the class module • The sub procedures and functions should be defined as Public 26 © 2001 by The Mc. Graw-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 – Write code between the 2 statements 27 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating a New Object Using a Class • Similar to creating a new tool for the toolbox but not yet instantiating • Declare a variable for the new object with datatype of the class • Then, instantiate the object using the New keyword 28 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Best Practices • You may declare and insttantiate an object at the same time but not best practice • Should declare the variable separately in the Declarations section • Instantiate the object – Only when(if) it is needed – Inside a Try/Catch block for error handling (Try/Catch block must be inside a procedure) 29 © 2001 by The Mc. Graw-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 Shared Methods can also be created 30 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Constructors and Destructors • Constructor – Method that automatically executes when an object is instantiated – Create by writing a Public Sub New procedure • Destructor – Method that automatically executes when an object is destroyed – Create by writing a Finalize procedure – Usage discouraged by Microsoft 31 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Overloading • Overloading means that 2 methods have the same name but a different list of arguments (the signature) • Create by giving the same name to multiple procedures in your class module, each with a different argument list 32 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Parameterized Constructor • Constructor that requires arguments • Allows arguments to be passed when creating an object • Can be used to assign initial property values 33 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Garbage Collection • Feature of. NET Common Language Runtime (CLR) that cleans up unused components • Periodically checks for unreferenced objects and releases all memory and system resources used by the objects • Microsoft recommends depending on Garbage Collection rather than Finalize procedures 34 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance Implemented • 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 35 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Overriding Methods • Methods created in subclass with the same name and the same argument list • Subclass will use the method in its own class rather than that in the base class • To override a method – Declare the base class method with the Overridable keyword – Declare the subclass method with the Overrides keyword 36 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating a Base Class Strictly for Inheritance • Classes can be created strictly for inheritance and are never instantiated • Subclasses are created and instantiated which inherit the base class properties and methods • For such a base class include the Must. Inherit modifier on the class declaration 37 © 2001 by The Mc. Graw-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. Form • Subclass from inherits from Base form 38 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating Inherited Form Class • Project menu, Add Windows Form • Modify the Inherits Statement to inherit from bass form using project name as the namespace OR • Project menu, Add Inherited Form • In dialog select name of Base form 39 © 2001 by The Mc. Graw-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 Form. Instance. Control. Name. Property • Example Dim frm. Sum. Instance as New frm. Sum( ) frm. Sum. Instance. lbl. Total. Text=Format. Currency(mdec. Total) 40 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object Browser • Use it to view the names, properties, methods, events and constants of VB objects, your own objects, and objects available from other applications • Accessed – Tab in Editor Window – View menu, Other Window, Object Browser 41 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Opening Object Browser from Toolbar 42 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object Browser Objects list Namespace icons Browse list Find Symbol Member list Method icon Property icon Constants icon Event icon Class icon 43 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Examining VB Classes Members of System. Windows. Forms. Message. Box Class 44 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Examining VB Classes (cont. ) Display the Message. Box. Buttons Constants 45 © 2001 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
- Slides: 45