Matakuliah Tahun : T0063 – Pemrograman Visual : 2009 Reducing Program Complexity General Sub Procedures and Developerdefined Functions CHAPTER SIX 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. Bina Nusantara University 2 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. Bina Nusantara University 3 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. Bina Nusantara University 4 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. Bina Nusantara University 5 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. Bina Nusantara University 6 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. Bina Nusantara University 7 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. Bina Nusantara University 8 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. Bina Nusantara University 9 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? Bina Nusantara University 10 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. Bina Nusantara University 11 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. Bina Nusantara University 12 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. Bina Nusantara University 13 6.2 Procedures with Parameters (cont.) • General Sub Procedures versus Event Procedures and the Object Paradigm – We have created a form class. – We also created two methods. • One was event procedure. • The other was a general sub procedure. Bina Nusantara University 14 6.3 Developer-Defined Functions • • • • Perform calculations or string manipulations. Return values. Use Return statements. Function headings include type specifications. Bina Nusantara University 15 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. Bina Nusantara University 16 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. Bina Nusantara University 17 6.4 Code Modules (cont.) • Libraries – Programmers organize reusable code for easy access. – A collection of code modules in categories is called a library. Bina Nusantara University 18 6.5 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. Bina Nusantara University 19