570 likes | 584 Views
Learn about the importance of procedures in program development, types of procedures, object-oriented programming, and hands-on tutorials with Visual Basic. Enhance your coding skills with this comprehensive guide.
E N D
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 • 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
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
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
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
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:
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
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
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
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
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
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
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
Application Output • Application output for the first six terms of the Fibonacci series: Subroutine Procedure for Fibonacci Series: Application Window
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
Function Procedures • Function procedures are like subroutine procedures except that they must return a value to the caller • Declaration syntax: • Example: Reverse Digits Procedure
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
Adding Code • Associate the code shown below with the Click event of the cmdTestFunction button. Function Procedure Example for Fibonacci Series: Code Window
Adding Code (cont.) • Write a function procedure called Fibseries as shown below. Function Procedure Example for Fibonacci Series: Code Window
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
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
Pass-by-Reference - Example (cont.) An Example of Pass-by-Reference: Code Window
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
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
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
Adding Code • Associate the code below with the Click event of the command button. Fibonacci Series Module Example: Code Window for Subroutine Procedure
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
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
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
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:
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
Adding Class Methods • To add methods to a class, we write procedures inside a class declaration Student Class Data Members Student Class Methods
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:
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
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.
Instantiation - Example Instantiating the Student Class: Code Window Using Methods of the Student Class: Application Output
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
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:
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
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:
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
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
Exception Handling - Example Division Subroutine Procedure: Without Exception Handling Division Subroutine Procedure: Runtime Error Due to an Overflow Exception
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
Try-Catch - Example • Handling a “division by zero” exception: A Try-Catch Example for Division Subroutine Procedure: Code Window
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
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
Adding Code • Associate the code below with the cmdInitArray command button. A Multiple Catch Blocks Example: Code Window
Adding Code (cont.) • Associate the code below with the cmdAddElements button. The Try-Catch-Finally Example: Code Window
Application Output • Sample application output for an exception: The Try-Catch-Finally Example: Application Output