1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Tutorial 12: Enhancing Excel with Visual Basic for Applications
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
The Web Warrior Guide to Web Design Technologies
Chapter 7: Sub and Function Procedures
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
VBA Modules, Functions, Variables, and Constants
Example 2.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
ASP.NET Programming with C# and SQL Server First Edition
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Review of C++ Programming Part II Sheng-Fang Huang.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
IE 212: Computational Methods for Industrial Engineering
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Tutorial 11 Using and Writing Visual Basic for Applications Code
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
JavaScript, Fourth Edition
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Introduction to Exception Handling and Defensive Programming.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Introduction to Programming with RAPTOR
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Controlling Program Flow with Decision Structures.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter Eleven Handling Events.
Microsoft Access Illustrated
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
VISUAL BASIC.
Chapter 4 void Functions
CIS16 Application Development Programming with Visual Basic
Topics Introduction to Functions Defining and Calling a Function
Methods.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304) West 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  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  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:

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  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: 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 Procedure 1.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  Summary

16 Function Procedures  Function procedures are like subroutine procedures except that they must return a value to the caller –Declaration syntax: –Example: Reverse Digits Procedure

17 Hands-On Tutorial: Working with Function Procedures  How-to: Write a Function Procedure 1.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  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

25 Hands-On Tutorial: Working with Visual Basic Modules  How-to: Write a Re-usable Standard Visual Basic Module 1.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  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:

31 Adding Class Modules to a Project  How-to: Add Class Modules to a Project 1.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 Class Methods 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:

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.

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:

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  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:

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  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: 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 Handling 1.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  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  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.