CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Tutorial 11 Using and Writing Visual Basic for Applications Code
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Multiple Forms, Standard Modules, And Menus
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
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
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CHAPTER THREE Representing Data: Constants and Variables.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter 6 Sub Procedures
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
CHAPTER THREE Representing Data: Constants and Variables.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Sub Procedures And Functions
Programming Right from the Start with Visual Basic .NET 1/e
IS 350 Application Structure
Using Procedures and Exception Handling
Variables and Arithmetic Operations
Chapter 6 Sub Procedures
CIS16 Application Development and Programming using Visual Basic.net
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Brief description on how to navigate within this presentation (ppt)
Presentation transcript:

CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Introduction Three important consideration help us design, construct, and maintain complex programs: 1. Break complex tasks into smaller “subtasks.” 2. Give each subtask a descriptive name. 3. Find processing tasks that have subtasks in common.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Objectives Share code by creating general sub procedures and functions. Use parameters to share data between procedures and functions. Use code modules to organize code for reusability. Use the KeyPress, Enter, and Leave events. Use the concept of form modality. Create and program main menus.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures We must be very precise in writing the criterion and alternative actions for decisions. In a program, –A condition is represented as an expression. –An outcome is the result of an evaluated condition. –An appropriate action follows the outcome.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) Using General Sub Procedures in a Project –Eliminate inconsistencies by placing common statements in a general sub procedure. –Apply a descriptive name for the sub procedure.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved A Sub Procedure Public Sub displayTime() Dim TheTime As String = Format(Now(), "Medium Time") MsgBox(TheTime, MsgBoxStyle.Information, "Current Time") End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) Execution of General Sub Procedures –Names of event procedures always end with an underscore followed by the type of event. –Names of general sub procedures do not. –A procedure call invokes a procedure.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Sub Procedure Execution Private Sub btnBeepTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBeepTime.Click MsgBox("MAKE NOISE") displayTime() MsgBox(“STOP NOISE") End Sub Public Sub displayTime() Dim TheTime As String = Format(Now(), "Medium Time") MsgBox(TheTime, MsgBoxStyle.Information, "Current Time") End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) Local Variables in General Sub Procedures –General sub procedures can access module-level and global variables. –They have their own variables. –Procedure-level variables are not related to other procedures.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) General Sub Procedures and Project Structure –Locating a General Sub Procedure in the Code Window We enter and view event procedures in the Code window. We also enter and view general sub procedures in the Code window. Scroll to find a procedure’s heading.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) –Procedure Scope Determines which procedure’s can invoke it. Private and Public. –Code Modules Can contain general sub procedures. Help to organize a project. Can be included in many different projects. –Project Structure See Figure 6.13 of textbook.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved General Sub Procedures (cont.) Creating General Sub Procedures –Start from the Code window of a form or code module. –Enter header for the sub procedure. –Enter general sub procedure statements.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters Drawbacks of Module-Level and Global Variables –Public access can mean trouble for some variables. –Procedures wanting to share data using the global variable have to “know” its name.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) Parameter Passing –Sending data from the calling to the called procedure. –Parameter Lists List of data items a procedure expects any calling procedure to send it. –Procedure Calls with Parameters See Figure 6.16 in textbook.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Passing Parameters Private Sub btnButton_Click(…) Handles … displayName(“Apple”) End Sub Public Sub displayName(ByVal name As String) MsgBox("My name is " & name) End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) –Analyzing Procedures That Use Parameters Hand-Check parameter passing.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) Multiple Parameters –A parameter list is key. –Every parameter list should contain: Number of arguments. Types of arguments. Sequence of arguments. Names arguments are referred to. What the parameters and arguments represents?

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) Passing by Reference and Passing by Value –ByRef keyword is short for “By Reference.” Parameter and argument refer to the same variable. Only way a sub procedure can change the value it is passed. –ByVal keyword is short for “By Value.” Specifies that the called procedure cannot change the value stored in a variable passed to it. Parameter is a local copy of the passed argument.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved ByVal Vs ByRef Private Sub btnButton_Click(…) Handles btnButton.Click Dim n As String = "Apple" displayName1(n) MsgBox("New name is " & n) displayName2(n) MsgBox("New name is " & n) End Sub Public Sub displayName1(ByVal name As String) MsgBox("My name is " & name) name = name & " Ha Ha" End Sub Public Sub displayName2(ByRef name As String) MsgBox("My name is " & name) name = name & " Ha Ha" End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) Passing Expressions –Calling procedure can pass an expression. Correcting Common Mistakes in Parameter Passing –Arguments Not Specified Number of arguments must match parameters. –Invalid Cast Exception Parameter and argument type mismatch.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Argument Type Mismatch Private Sub btnButton_Click(…) Handles … displayName(“Apple”) End Sub Public Sub displayName(ByVal name As Integer) MsgBox("My name is " & name) End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) –Arguments Out of Order Arguments and parameters must be associated by position, not by name. –Conflict between Parameter Name and Local Variable Name Parameter name and local variable names must be different.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Procedures with Parameters (cont.) General Sub Procedures versus Event Procedures –An event procedure is always associated with a control. –Developers create the procedure heading for a general sub procedure. –Visual Basic.NET creates the procedure heading for an event procedure.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Developer-Defined Functions Perform calculations or string manipulations. Return values. Use Return statements. Function headings include type specifications.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Developer-Defined Functions (cont.) Creating Functions –Follow the same steps to create a general sub procedure. –Use the keyword Function instead of Sub.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Function Call Private Sub btnButton_Click(…) Handles btnButton.Click Dim firstName As String = "Apple" Dim lastName As String = "Chan" Dim fullName As String fullName = getName(firstName, lastName) MsgBox(“My name is " & fullName) End Sub Public Function getName(ByVal fn As String, ByVal ln As String) As String Return ln & " " & fn End Function

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Code Modules Sub Main –Use to begin execution of the program by executing a general sub procedure. –Select in the Misc Property Pages dialog box under the Project menu. –You must create a public general sub procedure named Main in a code module.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Code Modules (cont.) Libraries –Programmers organize reusable code for easy access. –A collection of code modules in categories is called a library.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The KeyPress Event Enables your programs to respond to keystrokes made by the user. Any control that can have the focus is able to respond to this event.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The KeyPress Event (cont.) The KeyPress Event for TextBox Controls –User presses a key. –Visual Basic.NET stores ANSI character of the key. –Visual Basic.NET begins KeyPress event procedure. –The code will determine the disposition of the keypress.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The KeyPress Event (cont.) –The SendKeys Class Used to send any keystroke to an application. Keystrokes are represented by codes.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved KeyPress Event Handler Private Sub txtSSN_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSSN.KeyPress Dim N As Short = Len(txtSSN.Text) Select Case e.KeyChar Case BACKSPACE ' OK any time Case "0" To "9" ' digits …… End Select End Sub

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The Enter and Leave Events Enter event occurs for a control when the control receives the focus. Leave event occurs for a control when the control loses the focus. Both events are triggered by the user or by code. Help to make an application more intuitive for the user.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Modal versus Modeless Forms Most real-world applications use multiple forms. Form modality controls how a form is displayed. –When a modal form is shown, other forms become inactive. objFrmA.ShowDialog() –When a modeless form is displayed, both its controls and the controls on other forms are active. objFrmA.Show()

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved Modal versus Modeless Forms (cont.) Modal Forms –A message box is the simplest example. –User must click OK to return control to the procedure. Presenting a Sequence of Forms –Using modeless forms Control code must be written within each form. –Using modal forms Control of all forms can be in a one procedure.

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved The MainMenu Control Start by right-clicking on the MainMenu control in the component tray. Then select Edit Menu from the pop-up menu. Enter both main and sub menu items.