1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin...

57
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin [email protected] IE 423 Design of Decision Support Systems (304) 293-0388 West Virginia University

Transcript of 1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin...

1

Web-Enabled Decision Support Systems

Objects and Procedures

Don McLaughlin [email protected] 423 Design of Decision Support Systems (304) 293-0388West Virginia University

2

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

3

Introduction

Procedures allow us to group a block of statements that perform a specific small task– Facilitate design, development, and maintenance of large programs

– A typical program contains multiple procedures to solve a complex problem

Modules are containers for procedures and declarations commonly accessed by other modules within an application – Enhance software reusability and avoid duplicate development

– Class modules help us develop reusable modules Objects can be thought as instances of a class

Object-oriented programming involves designing program modules around classes and objects

4

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

5

Procedures

A procedure is a block of Visual Basic statements that accomplishes a specific task – The Divide and Conquer principle is to break up a complex task into

multiple, simpler sub-tasks

– Modular programming is the coding of such sub-tasks in classes, modules, subroutines, and functions

Example of a Procedure

6

Procedures Tasks

There are two important tasks associated with procedures: – Declaring procedures

Marking starting and ending statements Naming the procedure and its input parameters Example:

– Calling procedures Executing the statements enclosed by the procedure Example:

Sub Multiply (a As Integer, b As Integer) MessageBox.Show (a*b) End Sub

Multiply (10, 20)

7

Why Use Procedures?

Ease of programming: – Modular programming makes it easier to accomplish large and complex tasks

Division of labor: – Breaking up a huge task into smaller sub-tasks facilitates the division of labor

in a team development environment

Code reusability: – We can reuse the procedures developed for one task while writing code for

another task

8

Types of Procedures

Subroutine procedures: – Execute the statements specified in the procedure body – Do not return any values to the caller

Function procedures:– Execute the statements specified in the procedure body – Returns a value to the caller

Event procedures: – Special subroutines that are automatically invoked in response to a specific

event triggered by the user’s actions

Property procedures: – Contain two sub-procedures:

Get function procedure Set subroutine procedure

9

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

10

Subroutine Procedures

A subroutine procedure is a block of Visual Basic statements enclosed by the Sub and End Sub statements– Declaration syntax:

– Example:

Sub <Subroutine Name> (Comma separated input parameter list) Body of the subroutine End Sub

Factorial Subroutine Procedure

11

Hands-On Tutorial: Working with Subroutine Procedures

We will create an application that displays a Fibonacci series:– 0, 1, 1, 2, 3, 5, 8, 13, 21, …

How-to: Write a Subroutine Procedure1. Create a new Windows application named ProceduresObjects.

2. Design the default form, Form1, as shown below. Add a TextBox control named txtNoElements.

3. Add a Button control named cmdTestSub and a ListBox control named lstFibSeries.

Subroutine Procedure for Fibonacci Series: Design Window

12

Creating Subroutine Procedures

4. Create a subroutine procedure, FibSeriesSub, which generates a Fibonacci series of n terms, where n is the user-defined input parameter.

Subroutine Procedure for Fibonacci Series: Code Window

13

Adding Code

5. Call the FibSeriesSub subroutine procedure from the code for the Click event of the cmdTestSub command button.

Subroutine Procedure for Fibonacci Series: Code Window

14

Application Output

– Application output for the first six terms of the Fibonacci series:

Subroutine Procedure for Fibonacci Series: Application Window

15

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

16

Function Procedures

Function procedures are like subroutine procedures except that they must return a value to the caller– Declaration syntax:

– Example:

Function <Name> (Comma separated argument list) As <ReturnDataType> Body of the Function Return <Value> End Function

Reverse DigitsProcedure

17

Hands-On Tutorial: Working with Function Procedures

How-to: Write a Function Procedure1. Add a command button, cmdTestFunction, to Form1 of the

ProceduresObjects application as shown below.

Function Procedure Example for Fibonacci Series

18

Adding Code

2. Associate the code shown below with the Click event of the cmdTestFunction button.

Function Procedure Example for Fibonacci Series: Code Window

19

Adding Code (cont.)

3. Write a function procedure called Fibseries as shown below.

Function Procedure Example for Fibonacci Series: Code Window

20

Pass-by-Value and Pass-by-Reference

We pass input parameters to procedures in two different ways:– Pass-by-Value

Done via the ByVal keyword The caller procedure makes a copy of input parameters before passing them to the

callee procedure The callee procedure cannot modify the contents of the parameters as viewed by

the caller procedure

– Pass-by-Reference Done via the ByRef keyword The caller passes the pointer or memory location of the input parameter to the

callee The callee procedure can modify the input parameters as viewed by the caller

procedure

21

Pass-by-Reference - Example

In the two previous hands-on tutorials, we implemented the Fibonacci series using the Pass-by-Value option– We now illustrate the function procedure with the Pass-By-Reference option

An Example of Pass-by-Reference: Code Window

22

Pass-by-Reference - Example (cont.)

An Example of Pass-by-Reference: Code Window

23

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

24

Visual Basic Modules

Modules are containers for procedures and declarations commonly accessed by other modules within an application – Enhance software reusability and avoid duplicate development

– Syntax:

Class modules form the foundation of object-oriented programming in Visual Basic – By default, an application consists of a single form class module

Module <Module Name> Body of the module End Module

25

Hands-On Tutorial: Working with Visual Basic Modules

How-to: Write a Re-usable Standard Visual Basic Module1. Add another form, Form2, to the ProceduresObjects application and

establish it as the start-up form.

2. Design the form as shown below.

Fibonacci Series Module Example: Design Window

26

Adding Code

3. Associate the code below with the Click event of the command button.

Fibonacci Series Module Example: Code Window for Subroutine Procedure

27

Adding a Module

4. Choose Project | Add Module from the Main menu to open the Add New Item dialog box. Name the module FibonacciModule, and click Open button to add the module.

Adding a New Standard Module

28

Adding Code to a Module and Running

5. Use the code below for the module code. Save and run the application.

Fibonacci Series Module Example: Code Window for Module

29

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

30

Visual Basic Classes

Classes are symbolic representations of objects– Describe the properties, methods, and events that make up objects

Data members are the variables declared in a class Methods are the procedures of a class

– Can be viewed as user-defined data types

– Class declaration syntax:Class <Class Name> Body of the Class End Class

31

Adding Class Modules to a Project

How-to: Add Class Modules to a Project1. Choose the Project | Add Class option from the Main menu to open the Add

New Item dialog box.

2. Name the new class as Student, and click the OK button to add a new class tab in the Design Window.

3. Use the code below to add class data members.

Student Class Data Members

32

Adding Class Methods

To add methods to a class, we write procedures inside a class declaration

Student ClassMethods

Student Class Data Members

33

Constructor of a Class

A constructor is a special method of a class that creates a new object of the class when called– Instantiation is the creation of a new instance of a class

– In most cases, the constructor also populates the data members of the object

– Class constructor syntax:

Public Sub New (Argument list) Body – Initialization End Sub

34

Constructor Definition

We use the subroutine procedure New to define a class constructor– The arguments of the constructor (if any) can be used to populate the class

data members

– We can have multiple constructors with different input parameters

The Constructors for the Student Class

35

Instance of a Class

Objects are instances of a class – A class object is created by calling its constructor

– Examples:

– Notes: The first statement calls a constructor with two string arguments, instantiating an

object that has a valid first and last name. Other data members are assigned default values.

The second statement populates all the data members with valid values.

Dim student1 As New Student (“John”, “Doe”)

Dim student2 As New Student (111, “Chris”, “Doe”, “1/1/1978”, 4.0)

36

Instantiation - Example

Instantiating the Student Class: Code Window

Using Methods of the Student Class: Application Output

37

Property Procedures

Property procedures are blocks of code declared within property definitions– Visual Basic offers two types of property procedures:

Get: To get the property value Set: To assign a value to the property

– Example:

The BirthDate Property of the Student Class

38

Using Properties

We use a property by its name– When a property name is used with the equal (=) assignment operator, it

automatically calls the Set block of the property

– If we use the property name in an expression, it automatically calls the Get block of the property

– Examples:

Dim Student3 As New Student (“John”, “Adams”) ' New Object

Student3.BirthDate = “1/1/1978” ' Calls the Set Block

MessageBox.Show (Student3.BirthDate) ' Calls the Get Block

39

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

40

Navigating Through Application Forms

Windows forms are special class modules – To open or close a form:

Create an instance of a form object using its empty constructor Then use its Open method to open the form or the Close method to close it

Example:

– To close a form that we are already working inside: Make a use of self-reference, Me, rather than creating a new instance

Example:

Dim testForm As New <FormName>

testForm.Open()

testForm.Close()

Me.Close() 'Closes the current form

41

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

42

Exception Handling

Runtime errors are also known as exceptions

Exception handling is the technique of anticipating exceptions and accordingly manipulating the code to provide user-friendly error messages

Visual Basic offers two approaches for exception handling: – Unstructured exception handling

Almost obsolete from the Visual Basic language

– Structured exception handling Common technique that is supported by almost all existing object-oriented

programming languages Employs the Try-Catch structure to handle exceptions

43

Exception Handling - Example

Division Subroutine Procedure: Without Exception Handling

Division Subroutine Procedure: Runtime Error Due to an Overflow Exception

44

Using a Try-Catch Block

The Try-Catch structure is used to handle exceptions– Write our code inside a Try block

Throws an error object, e, if an exception occurs

– The Catch block handles any exceptions from the Try block Catches e as the type Exception Usually outputs a user-friendly error message Catch block syntax:

Catch <name> As <Exception Type> Body of the Catch Block

The Try-Catch Structure

45

Try-Catch - Example

Handling a “division by zero” exception:

A Try-Catch Example for Division Subroutine Procedure: Code Window

46

Using Multiple Catch Statements

Every Try block requires at least one Catch block– However, inside the Try block, we can incorporate as many Catch blocks for

different exceptions as we need

Representation of Multiple Catch Blocks

47

Hands-On Tutorial: Using the Try-Catch Block for Exception Handling

How-to: Write a Try-Catch Block for Exception Handling1. Add a form, Form3, to the ProceduresObjects application and design it as

shown below. Add two Button controls: cmdInitArray and cmdAddElements. Add four Label controls to display the capacity and size of the array. Add a ListBox control, lstArray to display the array contents.

2. Declare a dynamic array Arr and arrSize variables outside any procedure so that they can be accessible to the entire form class.

A Multiple Catch Blocks Example: Design Window

48

Adding Code

3. Associate the code below with the cmdInitArray command button.

A Multiple Catch Blocks Example: Code Window

49

Adding Code (cont.)

4. Associate the code below with the cmdAddElements button.

The Try-Catch-Finally Example: Code Window

50

Application Output

– Sample application output for an exception:

The Try-Catch-Finally Example: Application Output

51

Using the Finally Statement

The Finally statement appears after all the Catch blocks– If the Finally block is present, it is always executed

Whether or not the Try block throws an error or the control goes inside the Catch block(s)

The Try-Catch-Finally Structure

52

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

53

In-Class Assignment

A geometric progression is a sequence in which each term (except the first term) is derived by multiplying the preceding term by a non-zero constant referred as a common ratio. – Let the first term be a and the common ratio be r, then the general form of a

geometric progression is as follows: a, a*r, a*r2, a*r3, ...

– A geometric series is formed by a succession of terms in a geometric progression.

Develop a Visual Basic module to generate a geometric series: – The module should support a procedure GenerateGP that takes a, r, number

of desired terms n and a ListBox control as input parameters.

– The procedure should generate n terms of the series and populate the ListBox control.

– Test the module code from an application form.

– Also add exception-handling code as necessary.

54

Overview

12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures 12.5 Visual Basic Modules 12.6 Visual Basic Classes 12.7 Navigating Through Application Forms 12.8 Exception Handling 12.9 In-Class Assignment 12.10 Summary

55

Summary

A procedure is a block of Visual Basic statements that accomplishes a specific task. – A procedure call consists of a call to a procedure by its name and a list of

input argument values.

There are four types of procedures in Visual Basic:– Subroutine procedures:

Execute statements specified in the body of the subroutine procedure but do not return any value to the caller procedure.

– Event procedures: Like subroutine procedures except that they are executed in response to an event

triggered by a user action.

– Function procedures: Execute statements specified in the function procedure body and return a value to

the caller procedure.

– Property procedures: Procedures that contain two sub-procedures, a Get function procedure and a Set

subroutine procedure.

56

Summary (cont.)

Visual Basic code is stored in modules: a standard module or a class module. – A standard module (or simply, module) can contain any number of declaration

statements and procedures.

– Modules are containers for procedures and declarations commonly accessed by other modules within the application.

Classes are symbolic representations of objects.– They describe the properties, methods, and events that make up objects in

the same way that a blueprint describes the components and structure of a building.

57

Summary (cont.)

Runtime errors are also known as exceptions. – They can be dealt with through exception handling, the technique of

anticipating exceptions and accordingly manipulating the code to provide friendly error messages.

The keywords Try and End Try are used to specify a Try block. – A Catch statement must be used inside the Try block to catch and handle the

exceptions that might occur.

– Every Try block requires at least one Catch block. However, we can incorporate as many Catch blocks inside a Try block as needed

for handling different exceptions.

– The Finally statement appears after all the Catch blocks. If the Finally statement is present, it is always executed, whether or not the Try

block throws an error.