CHAPTER SIX Reducing Program Complexity General Sub Procedures
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
3 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 Key. Press, Enter, and Leave events. • Use the concept of form modality. • Create and program main menus. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
4 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
5 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
6 A Sub Procedure Public Sub display. Time() Dim The. Time As String = Format(Now(), "Medium Time") Msg. Box(The. Time, Msg. Box. Style. Information, "Current Time") End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
7 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
8 Sub Procedure Execution Private Sub btn. Beep. Time_Click(By. Val sender As System. Object, By. Val e As System. Event. Args) Handles btn. Beep. Time. Click Msg. Box("MAKE NOISE") display. Time() Msg. Box(“STOP NOISE") End Sub Public Sub display. Time() Dim The. Time As String = Format(Now(), "Medium Time") Msg. Box(The. Time, Msg. Box. Style. Information, "Current Time") End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
9 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
10 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
11 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
12 6. 1 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
13 6. 2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
14 6. 2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
15 Passing Parameters Private Sub btn. Button_Click(…) Handles … display. Name(“Apple”) End Sub Public Sub display. Name(By. Val name As String) Msg. Box("My name is " & name) End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
16 6. 2 Procedures with Parameters (cont. ) – Analyzing Procedures That Use Parameters • Hand-Check parameter passing. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
17 6. 2 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? Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
18 6. 2 Procedures with Parameters (cont. ) • Passing by Reference and Passing by Value – By. Ref 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. – By. Val 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
By. Val Vs By. Ref Private Sub btn. Button_Click(…) Handles btn. Button. Click Dim n As String = "Apple" display. Name 1(n) Msg. Box("New name is " & n) display. Name 2(n) Msg. Box("New name is " & n) End Sub Public Sub display. Name 1(By. Val name As String) Msg. Box("My name is " & name) name = name & " Ha Ha" End Sub Public Sub display. Name 2(By. Ref name As String) Msg. Box("My name is " & name) name = name & " Ha Ha" End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved. 19
20 6. 2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
21 Argument Type Mismatch Private Sub btn. Button_Click(…) Handles … display. Name(“Apple”) End Sub Public Sub display. Name(By. Val name As Integer) Msg. Box("My name is " & name) End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
22 6. 2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
6. 2 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved. 23
24 6. 3 Developer-Defined Functions • • Perform calculations or string manipulations. Return values. Use Return statements. Function headings include type specifications. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
25 6. 3 Developer-Defined Functions (cont. ) • Creating Functions – Follow the same steps to create a general sub procedure. – Use the keyword Function instead of Sub. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
26 Function Call Private Sub btn. Button_Click(…) Handles btn. Button. Click Dim first. Name As String = "Apple" Dim last. Name As String = "Chan" Dim full. Name As String full. Name = get. Name(first. Name, last. Name) Msg. Box(“My name is " & full. Name) End Sub Public Function get. Name(By. Val fn As String, By. Val ln As String) As String Return ln & " " & fn End Function Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
27 6. 4 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
28 6. 4 Code Modules (cont. ) • Libraries – Programmers organize reusable code for easy access. – A collection of code modules in categories is called a library. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
29 6. 5 The Key. Press 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
30 6. 5 The Key. Press Event (cont. ) • The Key. Press Event for Text. Box Controls – User presses a key. – Visual Basic. NET stores ANSI character of the key. – Visual Basic. NET begins Key. Press event procedure. – The code will determine the disposition of the keypress. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
31 6. 5 The Key. Press Event (cont. ) – The Send. Keys Class • Used to send any keystroke to an application. • Keystrokes are represented by codes. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
32 Key. Press Event Handler Private Sub txt. SSN_Key. Press(By. Val sender As Object, By. Val e As System. Windows. Forms. Key. Press. Event. Args) Handles txt. SSN. Key. Press Dim N As Short = Len(txt. SSN. Text) Select Case e. Key. Char Case BACKSPACE ' OK any time Case "0" To "9" ' digits …… End Select End Sub Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
33 6. 6 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
34 6. 7 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. • obj. Frm. A. Show. Dialog() – When a modeless form is displayed, both its controls and the controls on other forms are active. • obj. Frm. A. Show() Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
35 6. 7 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. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
36 6. 8 The Main. Menu Control • Start by right-clicking on the Main. Menu control in the component tray. • Then select Edit Menu from the pop-up menu. • Enter both main and sub menu items. Mc. Graw Hill/Irwin © 2002 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
- Slides: 36