230 likes | 439 Views
Writing General Procedures. Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur when the user can select either a command button or a menu option to do the same thing
E N D
Writing General Procedures • Often you will encounter programming situations in which multiple procedures perform the same operation • This condition can occur when the user can select either a command button or a menu option to do the same thing • Instead of retyping the code, you can write reusable code in a general procedure and call it from both event procedures
General Procedures • General procedures respond when specifically called by other procedures • They are not event driven • They are used to “package” a commonly used series of instructions • Invoke general procedures by calling them • Select Tools, Add Procedure to insert one
Creating a New Sub Procedure • STEP 1:Display the Code window for the form • STEP 2:Select Add Procedure from the Tools menu • STEP 3:Enter a name in the Add Procedure dialog box • STEP 4: SelectPrivate for Scope. Choosing Public makes a procedure available from other project modules. Note: Leave the Type set to sub for now • STEP 5: Click OK
Unlike event procedures you have used up to this point, general procedures must be explicitly called. • You can create procedures or functions • Functions return a single value, procedures do not • Both functions and sub procedures can have arguments • You pass information into a sub procedure or a function through its arguments
Arguments must be typed in the prototype: • Private Sub DoSomething(Arg1 as String, Arg2 as Integer) • ••• • End Sub • Functions are typed in addition to their arguments: • Private Function CalcInterest(Arg1 as Currency) As Currency • CalcInterest = <expression> 'return answer this way • End Function • Place functions in same place as event sub procedures
Passing Variables to Procedures • You may need to use the value of a variable in one procedure, and also in a second procedure that is called from the first. You could declare the variable as module level, but that approach makes it visible to all other procedures. You can keep the scope as narrow as possible by declaring it locally and passing it to any called procedures
Functions vs. Sub Procedures • A sub procedure is a procedure that performs actions. • A function procedure may perform an action, but it also returns a value (the return value) to the point from which it is called
Examples of function declaration: Private Function curCommission(ByVal curSalesAmount As Currency) As Currency If curSalesAmount < 1000 Then curCommission = 0 ElseIF curSalesAmount <= 2000 Then curCommission = .15 * curSalesAmount Else curCommission = .20 * curSalesAmount End If End Function
Calling the Commission Function:Dim curSales as CurrencyIf IsNumeric(txtSales.Text) ThencurSales = Val(txtSales) lblCommission.Caption = curCommission(curSales)End If Note: Name of argument being passed
Name of argument received Private Function curCommission(ByVal curSalesAmount As Currency) As Currency If curSalesAmount < 1000 Then curCommission = 0 ElseIF curSalesAmount <= 2000 Then curCommission = .15 * curSalesAmount Else curCommission = .20 * curSalesAmount End If End Function
Notice in the preceding example that the argument named in the function does not have the same name as the argument named in the function definition. • When the function is called, a copy of curSales is passed to the function and is assigned to the named argument, curSales Amount. • As the calculations are done (inside the function), for every reference to curSalesAmount, the value of curSales is actually used.
Passing Variables to Procedures • You may need to use the value of a variable in one procedure and also in a second procedure that is called from the first • You could declare the variable as Module level, but that approach makes the variable visible to all procedures • To keep the scope of the variable as narrow as possible, consider declaring the variable as local and passing it to any called procedures
Passing arguments ByVal and ByRef • When you pass an argument you may pass it ByVal or ByRef. • The ByVal sends a copy of the argument’s value to the procedure so that procedure cannot alter the original value. • ByRef sends a reference indicating where the value is stored in memory, allowing the called procedure to actually change the argument’s original value.
Passing arguments ByVal and ByRef • You can specify how you want to pass the argument by using the ByVal or ByRef keyword before the argument. • If you don’t specify ByVal or ByRef, arguments are passed by reference. Private Sub Commission(ByVal curSalesAmount As Currency)
Len and InStr Functions • The functions Len and InStr operate on strings but produce numbers • The function Len gives the number of characters in a string. • The function InStr searches for the occurrence of one string in another and gives the position at which the string is found • Both functions return a value
Examples Len(“UCC”) is 3 Len(“University College Cork”) is 23 Len(“ ”) is 0 InStr(“Cork”, “University College Cork”) is 20 InStr(“ ”, “University College Cork”) is 11 InStr(“city”, “University College Cork”) is 0