Chapter 6 OOP Creating Object Oriented Programs Programming
Chapter 6 OOP: Creating Object. Oriented Programs Programming In Visual Basic. NET © 2004 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object-Oriented (OO) Program • Objects Class – Consist of one Met d o h hod or more data Met values which Data Met define the state Message d hod o h t or properties Me of the object – Encapsulated by a set of functions (methods) that can be applied to that object 2 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object-Oriented (OO) Program • Class – Defines: • Characteristics of the data contained by objects of the class • Functions that can be applied to the objects of the class Class Met hod t e M Met hod 3 Data hod t e M Met hod Met hod Data hod Met © 2005 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 Class Met Met d d Lemon hod hobe t hodor Chocolate – MFlavor property could t e etho e M M Met hod Data Object 4 hod Met hod Data hod Met Object © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
"Cookie Analogy" (continued) • Methods = Eat, Bake, or Crumble • Events = Cookie crumbling all by itself and informing you Distinction between method an event is somewhat fuzzy 5 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object-Oriented Terminology • • • 6 Encapsulation Inheritance Polymorphism Reusable Classes Multitier Applications © 2005 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 does not 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 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Encapsulation Class d o h t Me Message 8 Met hod Data d o h t Me © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance • Ability to create a new class from an existing class – Original class is called Base Class, Superclass, or Parent Class – Inherited class is called Subclass, Derived Class, or Child Class • For example, each form created is inherited from the existing Form class • Purpose of inheritance is reusability 9 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance (continued) • Examine first line of code for a form in the Editor Inherited Class: Subclass, Derived Class, Child Class Public Class Form 1 Inherits System. Windows. Form Original Class: Base Class, Superclass, Parent Class 10 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance Example • Base Class – Person • Subclasses – Employee – Customer – Student Person -Name -Address -Phone Properties Employee 11 Customer Student © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Polymorphism • Methods having identical names but different implementations • Overloading – Several argument lists for calling the method – 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 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Reusability • • • 13 Big advantage of OOP over traditional programming New classes created can be used in multiple projects Each object created from the class can have its own properties © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Multitier Applications • Classes are used 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 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Three-tier Model • Most popular implementation Presentation Tier Business Tier User Interface Forms Controls Menus Business Objects Validation Calculations Business Logic Business Rules 15 Data Tier Data Retrieval Data Storage © 2005 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 General Form New class. Name( ) 16 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
New Keyword Examples Dim arial. Font As Font = New Font ("Arial", 12) message. Label. Font = arial. Font OR message. Label. Font = New Font ("Arial", 12) 17 © 2005 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 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Specifying a Namespace (continued) • 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 a class in a different namespace – Write out the entire namespace or – Add an Imports statement at the top of the code to specify the namespace 19 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Designing Your Own Class • Analyze characteristics needed by new objects – Characteristics or properties are defined as variables – Define the properties as variables in the class module • Analyze behaviors needed by new objects – Behaviors are methods – Define the methods as sub procedures or functions 20 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating Properties in 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) 21 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Property Procedures • Properties in a class are accessed with accessor methods in a property procedure • Name used for property procedure is the name of the property seen by the outside world • Set Statement – Uses Value keyword to refer to incoming value for property – Assigns a value to the property • Get Statement – Retrieves a property value – Must assign a return value to the procedure name 22 © 2005 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 Return Class. Variable [Property. Name = Class. Variable] End Get Set (By. Value As Data. Type) [statements, such As validation] Class. Variable = Value End Set End Property 23 © 2005 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 the Get portion of the property procedure [Public] Read. Only Property. Name( ) As Data. Type 24 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Write-Only Properties • At times a property can be assigned by an object but not retrieved – Create a property block that contains only a Set to create a write-only property [Public] Write. Only Property. Name( ) As Data. Type 25 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Class Methods • Create methods by coding public procedures within a class • Methods declared with the Private keyword are available only within the class • Methods declared with the Public keyword are available to external objects 26 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Constructors and Destructors • Constructor – Method that automatically executes when an object is instantiated – Constructor must be public and is named New • Destructor – Method that automatically executes when an object is destroyed 27 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Overloading Methods • Overloading means that two 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 28 © 2005 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 29 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Create a New Class • Project, Add Class • In Add New Item dialog box, choose Class • Name the Class • Define the Class properties • To allow access from outside the class, add property procedures • Code the methods 30 © 2005 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 creating an instance of the class • Declare a variable for the new object • Then, instantiate the object using the New keyword Private a. Book. Sale As Book. Sale a. Book. Sale = New Book. Sale( ) Or Dim a. Book. Sale As New Booksale( ) 31 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating a New Object Using a Class (continued) • If object variable is needed in multiple procedures, delcare the object at class level • 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) • Pass values for the arguments at instantiation when using a parameterized constructor 32 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Instance Variables versus Shared Variables • Instance variables or properties – Separate memory location for each instance of the object • Shared variables or properties – Single variable 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 33 © 2005 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 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Inheritance • 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 • Use the Inherits statement following the class header and prior to any comments 35 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Overriding Methods • Methods with the same name and the same argument list as the base class • Derived class (subclass) will use the new method rather than the method in the base class • To override a method – Declare the original method with the Overridable keyword – Declare the new method with the Overrides keyword 36 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Creating a Base Class Strictly for Inheritance • Classes can be created strictly for inheritance by two or more similar classes and are never instantiated • For a base class that you intend to inherit from, include the Must. Inherit modifier on the class declaration • In each base class method that must be overridden, include the Must. Override modifier and no code in the base class method 37 © 2005 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 • New form inherits from Base form 38 © 2005 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 base form using project name as the namespace • OR • Project menu, Add Inherited Form • In dialog select name of base form 39 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Coding for Events of an Inherited Class • Copy procedure from base class into derived class and make modifications • An alternate way is to use the inherited event handler in the derived class 40 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Managing Multiclass Projects • VB projects are automatically assigned a namespace which defaults to the name of the project • Add an existing class to a project by copying the file into the project folder and then adding the file to the project – Project/Add Existing Item 41 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Displaying Values on a Different Form • Refer to controls on another form by using the identifier for the form instance • General Syntax Form. Instance. Control. Name. Property • Example Dim a. Summary. Form As New summary. Form( ) a. Summary. Form. Sales. Label. Total. Text = a. Book. Sales. Total. To. String("C") 42 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Using the 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 • To Display – Click on Tab in Editor Window – Object Browser toolbar button 43 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Object Browser Objects list Namespace icons Browse list Find Symbol Members list Method icon Property icon Constants icon Description Pane Class icon 44 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Examining VB Classes Members of System. Windows. Forms. Message. Box Class 45 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
Examining VB Classes (continued) Display the Message. Box. Buttons Constants 46 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
- Slides: 46