1 / 23

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

ernie
Télécharger la présentation

Writing General Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

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

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

  14. 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)

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

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

More Related