One Step Further: Passing Arguments by Value and by Reference


One Step Further: Passing Arguments by Value and by Reference

In the discussion of Sub and Function procedures, you learned that arguments are passed to procedures by value or by reference. Using the ByVal keyword indicates that variables should be passed to a procedure by value (the default). Any changes made to a variable passed in by value aren't passed back to the calling procedure. However, as you learned in the Text Box Sub program, using the ByRef keyword indicates that variables should be passed to a procedure by reference, meaning that any changes made to the variable in the procedure are passed back to the calling routine. Passing by reference can have significant advantages, as long as you're careful not to change a variable unintentionally in a procedure. For example, consider the following Sub procedure declaration and call:

Sub CostPlusInterest(ByRef Cost As Single, ByRef Total As Single)     Cost = Cost * 1.05  'add 5% to cost...     Total = Int(Cost)   'then make integer and return End Sub . . . Dim Price, TotalPrice As Single Price = 100 TotalPrice = 0 CostPlusInterest(Price, TotalPrice) MsgBox(Price & " at 5% interest is " & TotalPrice)

In this example, the programmer passes two single-precision variables by reference to the CostPlusInterest procedure: Price and TotalPrice. The programmer plans to use the updated TotalPrice variable in the subsequent MsgBox call but has unfortunately forgotten that the Price variable was also updated in an intermediate step in the CostPlusInterest procedure. (Because Price was passed by reference, changes to Cost automatically result in the same changes to Price.) This produces the following erroneous result when the program is run:

graphic

However, the programmer probably wanted to show the following message:

graphic

So how should the CostPlusInterest procedure be fixed to produce the desired result? The easiest way is to declare the Cost argument by using the ByVal keyword, as shown in the following program statement:

Sub CostPlusInterest(ByVal Cost As Single, ByRef Total As Single)

By declaring Cost using ByVal, you can safely modify Cost in the CostPlusInterest procedure without sending the changes back to the calling procedure. By keeping Total declared using ByRef, you can modify the variable that's being passed, and only those changes will be passed back to the calling procedure. In general, if you use ByRef only when it's needed, your programs will be freer of defects.

Here are some guidelines on when to use ByVal and when to use ByRef:

  • Use ByVal when you don't want a procedure to modify a variable that's passed to the procedure through an argument.

  • Use ByRef when you want to allow a procedure to modify a variable that's passed to the procedure.

  • When in doubt, use the ByVal keyword.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net