Download presentation
Presentation is loading. Please wait.
Published byTabitha Holmes Modified over 8 years ago
1
Sub Procedures Chapter 6-Part 1
2
Chapter 6 Part 12 Event Procedures Code that occurs based upon event. Mouse click Got focus Repetitive code that might be simplified. Similar events; same process
3
Chapter 6 Part 13 Real-World Applications Easily have thousands of lines of code. Very difficult to modify and maintain if code is not modularized.
4
Chapter 6 Part 14 General Sub Procedures Reusable code that can be used in > 1 procedure. Reduces chances of typos in retyping code. Simplifies program by avoiding duplication of code. Code that is not processed solely linked to a control, such as a button. Collection of statements that performs a task, such as general calculations and routine processing. Code that is “called” from event procedures or other general procedures. Call means to “invoke the procedure.” The procedure call executes the procedure.
5
Chapter 6 Part 15 Event Procedure vs. General Sub Procedure Event Procedure Private Sub btnProcess_Click() Control prefix & name, underscore, event General Sub Procedure Private Sub CalcTax() Not based on control; has own procedure name. Not based on event—called from other procedures.
6
Chapter 6 Part 16 Declaring a Sub Procedure AccessSpecifier gives accessibility to the program Private: only accessible by other procedures declared on the same form Public: accessible by procedures that are declared in the current form and by other forms ProcedureName reflects purpose of procedure; use consistent casing. ParameterList is a list of variable values that are being passed to the sub procedure 'Sub' and 'End' are keywords AccessSpecifier Sub ProcedureName (ParameterList) [Statement(s)] End Sub
7
Chapter 6 Part 17 Declaration Example Private Sub DisplayGrade() Limited accessibility to current form Designated as a sub procedure Pascal casing (similar to camel casing without a three-letter prefix)
8
Chapter 6 Part 18 Example of VB6 Want the Spouse contribution question grayed-out and the Spouse Yes/No options disabled when these option buttons are selected: Single Married Filing Separate Return Head of Household Qualifying Widow(er)
9
Chapter 6 Part 19 Example If user clicks one of these options Single Married Filing Separately Head of Household Widow(er) Then program does these consistent tasks: Disables Yes/No Spouse radio buttons. Grays out Spouse options.
10
Chapter 6 Part 110 Redundant Code for Single, Etc. Redundant code for all four event procedures
11
Chapter 6 Part 111 Revised Code as Sub Procedure Shared code typed once; Various event procedures call the general procedure Code that calls (invokes) sub procedure
12
Chapter 6 Part 112 Enable for Joint Return
13
Chapter 6 Part 113 Procedure Call w/in Event Procedure Private Sub radSingle_Click() WithdrawPECSpouseQuestion End Sub Or Private Sub radSingle_Click() Call WithdrawPECSpouseQuestion End Sub The keyword Call is optional; the code works with or without it.
14
Chapter 6 Part 114 Execution of General Sub Procedures 1) Transfers execution from event procedure or another general procedure to the general sub procedure. 2) Executes statements in the general sub procedure. 3) Returns execution to the event procedure and resumes with the event procedure’s next statement.
15
Chapter 6 Part 115 Sub Procedures Can… Access class-level (form-level) variables. Have their own local variables that are… Created when the sub procedure starts. Destroyed when the sub procedure ends. Not maintained from one procedure call to the next. Declared and initialized again when called again. Maintained for static local variables. Not destroyed when the procedure ends. Exist for lifetime of application. Scope limited to procedure itself, however. Static VariableName as DataType
16
Chapter 6 Part 116 Sub Procedures Do Not… Destroy local variables from procedure that calls sub procedure. Event procedure and general sub procedure maintain their own local variables for their duration.
17
Chapter 6 Part 117 Variable Scope Event procedure variables still in scope even when call procedure; go out of scope when event procedure terminates. Sub procedure variables in scope during execution—until End Sub.
18
Chapter 6 Part 118 Conceptual Exercise Dim X As Integer Private Sub btnScopeQuiz_Click() Dim Y As Integer Dim Z As Integer Y = 2 Z = 5 X = Y + Z MessageBox.Show(X & Y & Z) SQuizProcedureA SQuizProcedureB MessageBox.Show(X & Y & Z) SQuizProcedureA End Sub Private Sub SQuizProcedureA() Dim X As Integer Dim Y As Integer Y = Y + 4 X = X + Y MessageBox.Show(X & Y) End Sub Private Sub SQuizProcedureB() Dim Y As Integer Dim Z As Integer Y = Y - 1 Z = 3 X = X + Y + Z MessageBox.Show(X & Y & Z) End Sub
19
Chapter 6 Part 119 Tutorial 6-2
20
Chapter 6 Part 120 Program Specs Show Grade Gets user input and stores into variables. Calculates & displays test average. Determines & displays letter grade. Drop Lowest Test Score Gets user input and stores into variables. Drops lowest score, calculates & displays test average. Determines & displays letter grade.
21
Chapter 6 Part 121 Need 2 Sub Procedures for Common Tasks (used for both calc grade buttons) Gets scores and places them into variables. Displays the letter grade.
22
Chapter 6 Part 122 Validation Procedure
23
Chapter 6 Part 123 Gets User Input Procedure
24
Chapter 6 Part 124 Determines Grade Procedure
25
Chapter 6 Part 125 Show Grade Event Procedure
26
Chapter 6 Part 126 Drop Lowest Score Event Procedure
27
Chapter 6 Part 127 Procedures w/ Parameters Can pass data between the calling procedure and the general procedure. Way to share data between event procedures and general procedures (or between two general procedures). Offers more flexibility than by using module-level or global variables. Global or module variables allow access to be changed whereas some procedures should not be allowed to change variable contents.
28
Chapter 6 Part 128 Parameter Passing The calling procedure gives (passes) a variable to the procedure. Values sent into a procedure are called arguments. The called procedure uses the variable to do its own processing. The called procedure hands the variable back to the procedure that called it.
29
Chapter 6 Part 129 Passing by Value A copy of the argument is passed to the procedure. The procedure uses the copy to perform tasks. The procedure does not change the original argument.
30
Chapter 6 Part 130 Pass by Reference A argument itself is passed to the procedure. The procedure uses an alias of the variable being passed to perform tasks. The alias in the procedure does change the original argument. The alias actually points to the memory location of the argument variable itself, thus being able to change the argument variable.
31
Chapter 6 Part 131 Syntax Call ProcedureName ArgumentPassed Call Rotate(MyInitials)‘variable passed Private Sub ProcedureName(ParametersDeclared) Private Sub Rotate(TwoChars As String)
32
Chapter 6 Part 132 Passing Multiple Arguments Call Statement ShowSum(intValue1, strYourName) Procedure Private Sub ShowSum(ByVal intNum1 as Integer, ByVal strName as String)
33
Chapter 6 Part 133 Passing Rules Must use same data types in argument and parameter. May use different variable names in argument and parameter. Procedure uses parameter variable names declared within the procedure
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.