1 / 57

Web-Enabled Decision Support Systems

Web-Enabled Decision Support Systems. Objects and Procedures. Prof. Name name@email.com Position (123) 456-7890 University Name. Overview. 12.1 Introduction 12.2 Procedures 12.3 Subroutine Procedures 12.4 Function Procedures

meaganp
Télécharger la présentation

Web-Enabled Decision Support Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web-Enabled Decision Support Systems Objects and Procedures Prof. Name name@email.com Position (123) 456-7890 University Name

  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 ofVisual 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 • 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: 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 • Create a new Windows application named ProceduresObjects. • Design the default form, Form1, as shown below. Add a TextBox control named txtNoElements. • Add a Button control named cmdTestSub and a ListBox control named lstFibSeries. Subroutine Procedure for Fibonacci Series: Design Window

  12. Creating Subroutine Procedures • 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 • 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: Reverse Digits Procedure

  17. Hands-On Tutorial: Working with Function Procedures • How-to: Write a Function Procedure • Add a command button, cmdTestFunction, to Form1 of the ProceduresObjects application as shown below. Function Procedure Example for Fibonacci Series

  18. Adding Code • 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.) • 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

  25. Hands-On Tutorial: Working with Visual Basic Modules • How-to: Write a Re-usable Standard Visual Basic Module • Add another form, Form2, to the ProceduresObjects application and establish it as the start-up form. • Design the form as shown below. Fibonacci Series Module Example: Design Window

  26. Adding Code • 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 • 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 • 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:

  31. Adding Class Modules to a Project • How-to: Add Class Modules to a Project • Choose the Project | Add Class option from the Main menu to open the Add New Item dialog box. • Name the new class as Student, and click the OK button to add a new class tab in the Design Window. • 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 Data Members Student Class Methods

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

  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: 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 • 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. • 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 • Associate the code below with the cmdInitArray command button. A Multiple Catch Blocks Example: Code Window

  49. Adding Code (cont.) • 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

More Related