Writing General Procedures

23
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

description

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 - PowerPoint PPT Presentation

Transcript of Writing General Procedures

Page 1: Writing General Procedures

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

Page 2: Writing General 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

Page 3: Writing General Procedures

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: Select Private 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

Page 4: Writing General Procedures
Page 5: Writing General Procedures

•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

Page 6: Writing General Procedures

• 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

Page 7: Writing General 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

Page 8: Writing General 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

Page 9: Writing General Procedures

• 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 IfEnd Function

Page 10: Writing General Procedures

Calling the Commission Function:Dim curSales as CurrencyIf IsNumeric(txtSales.Text) Then

curSales = Val(txtSales) lblCommission.Caption = curCommission(curSales)

End If

Note: Name of argument being passed

Page 11: Writing General Procedures

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 IfEnd Function

Name of argument received

Page 12: Writing General Procedures

• 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.

Page 13: Writing General 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 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

Page 14: Writing General 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.

Page 15: Writing General Procedures

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)

Page 16: Writing General Procedures

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

Page 17: Writing General Procedures

ExamplesLen(“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

Page 18: Writing General Procedures
Page 19: Writing General Procedures
Page 20: Writing General Procedures
Page 21: Writing General Procedures
Page 22: Writing General Procedures
Page 23: Writing General Procedures