Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Receiving by Reference and by Value

Visual Basic lets you pass arguments two ways: by reference and by value. The way you use them determines whether the receiving procedure can change the arguments so that those changes remain in effect after the calling procedure regains control. If you pass and receive by reference (the default method), the calling procedure's passed local variables may be changed in the receiving procedure. If you pass and receive by value, the calling procedure can access and change its received arguments, but those changes don't retain their effects in the calling procedure.

When passing by reference, subroutines and functions can always use their received values and also change those arguments. If a receiving procedure changes one of its arguments, the corresponding variable in the calling procedure is also changed. Therefore, when the calling procedure regains control, the value (or values) that the calling procedure sent as an argument to the called subroutine may be different from the time before the call

By reference is a way in which you pass values and allow the called procedure to change those values, also called by address. By value is a way in which you pass values and protect the calling procedure's passed data so that the called procedure cannot change the original data.

Arguments are passed by reference, meaning that the passed arguments can be changed by their receiving procedure. If you want to keep the receiving procedure from being able to change the calling procedure's arguments, you must pass the arguments by value. To pass by value, precede any and all receiving argument lists with the ByVal keyword, or enclose the passed arguments in parentheses.

It's generally safer to receive arguments by value because the calling procedure can safely assume that its passed values won't be changed by the receiving procedure. Nevertheless, there may be times when you want the receiving procedure to permanently change values passed to it. In such cases, you'll need to receive those arguments by reference.

Listing 13.3 shows two subroutine procedures. One, named Changes(), receives arguments by address. The second procedure, NoChanges(), receives its arguments by value. Even though both procedures multiply their arguments by two, those changes affect the calling procedure's variables only when Changes() is called but not when NoChanges() is called.

Example 13.3. Some procedures can change the sending procedure's arguments.

 1: Sub Changes(N As Integer, S As Single)
 2: ' Receives arguments by reference
 3: N = N * 2 ' Double both
 4: S = S * 2 '   arguments
 5: ' When the calling routine regains control,
 6: ' its two local variables will now be twice
 7: ' as much as they were before calling this.
 8: End Sub
 9:
10: Sub NoChanges(ByVal N As Integer, ByVal S As Single)
11: ' Receives arguments by value
12: N = N * 2   ' Double both
13: S = S * 2     '   arguments
14: ' When the calling routine regains control,
15: ' its two local variables will not be
16: ' changed from their original values.
17: End Sub

As you can see, Changes() receives its arguments by reference. (Remember that the default passing method is by reference, even if you omit ByRef.) Therefore, when the procedure doubles the arguments, the calling procedure's argument variables change as well.

In NoChanges(), the procedure receives its arguments by value. Therefore, nothing NoChanges() does can change those values in the calling procedure.

Share ThisShare This

Informit Network