Chapter 6 OOP: Creating Object- Oriented Programs Programming In

Chapter 6

OOP: Creating Object-

Oriented Programs

Programming In

Visual Basic .NET

Object-Oriented (OO) Program

Objects

– Consist of one or more data values which define the state or properties of the object

Class

Message

Data

– Encapsulated by a set of functions (methods) that can be applied to that object

6- 2 © 2005 by The McGraw-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

Data Data Data

6- 3 © 2005 by The McGraw-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

Data Data Data

Object

6- 4

Object Object

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

"Cookie Analogy" (continued)

Methods = Eat, Bake, or Crumble

• Events = Cookie crumbling all by itself and informing you

6- 5

Distinction between method and an event is somewhat fuzzy

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Object-Oriented Terminology

Encapsulation

• Inheritance

• Polymorphism

• Reusable Classes

• Multitier Applications

6- 6 © 2005 by The McGraw-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

6- 7 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Encapsulation

Class

Message

Data

6- 8 © 2005 by The McGraw-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

6- 9 © 2005 by The McGraw-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 Form1

Inherits System.Windows.Forms.Form

6- 10

Original Class: Base Class, Superclass, Parent Class

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Inheritance Example

Base Class

– Person

• Subclasses

– Employee

– Customer

Student

Properties

Person

-Name

-Address

-Phone

Employee Customer Student

6- 11 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Polymorphism

Methods having identical names but different implementations

Overloading

Several argument lists for calling the method

– Example : MessageBox.Show method

• Overriding

– Refers to a class that has the same method name as its base class

– Method in subclass takes precedence

6- 12 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Reusability

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

6- 13 © 2005 by The McGraw-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

6- 14 © 2005 by The McGraw-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

Data Tier

Data Retrieval

Data Storage

6- 15 © 2005 by The McGraw-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

6- 16

New className( )

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

New Keyword Examples

Dim arialFont As Font = New Font ("Arial", 12) messageLabel.Font = arialFont

OR messageLabel.Font = New Font ("Arial", 12)

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

6- 18

Name of the Class

Public Class Form1

Inherits System.Windows.Forms.Form

Namespace

© 2005 by The McGraw-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

– 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

6- 19 © 2005 by The McGraw-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

6- 20 © 2005 by The McGraw-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)

6- 21 © 2005 by The McGraw-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

6- 22 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Property Procedure General Form

Private ClassVariable As DataType

[Public] Property PropertyName( ) As DataType

Get

Return ClassVariable [PropertyName = ClassVariable]

End Get

Set (ByVal Value As DataType )

[ statements, such As validation ]

ClassVariable = Value

End Set

End Property

6- 23 © 2005 by The McGraw-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 ReadOnly modifier

Write only the Get portion of the property procedure

6- 24

[Public] ReadOnly Property PropertyName( ) As DataType

© 2005 by The McGraw-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

6- 25

[Public] WriteOnly Property PropertyName( ) As DataType

© 2005 by The McGraw-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

6- 26 © 2005 by The McGraw-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

6- 27 © 2005 by The McGraw-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

6- 28 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Parameterized Constructor

Constructor that requires arguments

• Allows arguments to be passed when creating an object

6- 29 © 2005 by The McGraw-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

6- 30 © 2005 by The McGraw-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 aBookSale As BookSale aBookSale = New BookSale( )

Or

Dim aBookSale As New Booksale( )

6- 31 © 2005 by The McGraw-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

6- 32 © 2005 by The McGraw-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

6- 33

Shared Methods can also be created

© 2005 by The McGraw-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

6- 34 © 2005 by The McGraw-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

6- 35 © 2005 by The McGraw-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

6- 36 © 2005 by The McGraw-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

MustInherit modifier on the class declaration

In each base class method that must be overridden, include the MustOverride modifier and no code in the base class method

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

• New form inherits from Base form

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

6- 39 © 2005 by The McGraw-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

6- 40 © 2005 by The McGraw-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

6- 41 © 2005 by The McGraw-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

Example

FormInstance.ControlName.Property

Dim aSummaryForm As New summaryForm( ) aSummaryForm.SalesLabelTotal.Text = aBookSale.SalesTotal.ToString( " C " )

6- 42 © 2005 by The McGraw-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

6- 43 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Object Browser

Objects list

Browse list

Find

Symbol Members list

Namespace icons

Constants icon

Class icon

6- 44

Method icon

Property icon

Description Pane

© 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Examining VB Classes

Members of

System.Windows.Forms.MessageBox

Class

6- 45 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Examining VB Classes (continued)

Display the

MessageBoxButtons Constants

6- 46 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.