Chapter six Reducing program complexity General sub procedures and developerdefined functions. After completing this unit, you should be able to 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 menu.
Trang 1CHAPTER SIX
Reducing Program Complexity General Sub Procedures and Developer-defined Functions
Trang 2• 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.
Trang 3• 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
Trang 46.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.
Trang 56.1 General Sub Procedures (cont.)
– Eliminate inconsistencies by placing common
statements in a general sub procedure.
– Apply a descriptive name for the sub procedure.
Trang 66.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.
Trang 76.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.
Trang 86.1 General Sub Procedures (cont.)
• General Sub Procedures and Project Structure
– Locating a General Sub Procedure in the Code
Trang 96.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.
Trang 106.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.
Trang 116.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.
Trang 126.2 Procedures with Parameters (cont.)
– Procedure Calls with Parameters
• See Figure 6.16 in textbook.
Trang 136.2 Procedures with Parameters (cont.)
– Analyzing Procedures That Use Parameters
• Hand-Check parameter passing.
Trang 146.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?
Trang 156.2 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.
Trang 166.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.
Trang 176.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
Trang 18• 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.
Trang 196.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.
Trang 206.3 Developer-Defined Functions
• Perform calculations or string manipulations
• Return values
• Use Return statements
• Function headings include type specifications
Trang 216.3 Developer-Defined Functions (cont.)
Trang 226.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.
Trang 236.4 Code Modules (cont.)
Trang 246.5 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
Trang 256.5 The KeyPress Event (cont.)
• The KeyPress Event for TextBox Controls
– User presses a key.
– Visual Basic NET stores ANSI character of the
Trang 266.5 The KeyPress Event (cont.)
– The SendKeys Class
• Used to send any keystroke to an application
• Keystrokes are represented by codes
Trang 276.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
Trang 286.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.
– When a modeless form is displayed, both its
controls and the controls on other forms are
active.
Trang 296.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.
Trang 306.8 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
Trang 31Chapter Summary
• General sub procedures and developer-defined
functions break programming into small parts
• A general sub procedure performs a specific
processing task
• Developers can use a library of procedures
performing common tasks
• General sub procedures are not linked to any
control on a form
Trang 32Chapter Summary (cont.)
• General sub procedures can be called from
other general sub procedures or from event
procedures
• A developer-defined function returns a single
value when it executes
• Procedures and functions can use parameters to share data
• An argument can be passed to a procedure or
Trang 33Chapter Summary (cont.)
• The KeyPress event procedure has a parameter that determines the key that the user has
pressed
• The Enter event occurs when the user gives the focus to a control
• Form modality refers to the way forms react
when two or more forms are displayed at one
time
• Form modality is specified by the type of Show
method used
Trang 34Chapter Summary (cont.)
• Applications can incorporate a main menu bar
by using the MainMenu control