Difference Between ByVal and ByRef on Array Variables

The effect of ByVal and ByRef on arrays is slightly different than their effect on scalar variables such as Integer and Double.

As discussed in Module 8, an array variable points to the address in memory of an Array object. If an array is passed ByVal, the called procedure cannot change the array object to which the array variable points. However, it can change the values of the individual members of the array. If an array is passed ByRef, the called procedure can change either the array object to which the variable points or the values of the individual members of the array.

For example, the following subroutine, IncreaseByOne, increases by 1 the value of each element in the array passed to it by value:

Public Sub IncreaseByOne(ByVal A() As Long)

Dim J As IntegerFor J = 0 To UBound(A)

Next J

End Sub

If IncreaseByOne is passed by an array, the value of each element of that array in the called code will be increased by 1:

IncreaseByOne(N)

Thus after IncreaseByOne iscalled, the element values of the array N are now 11, 21, 31, and 41. The same result would occur if the arguments were passed ByRef.

Next, the following subroutine, ReplaceArray, assigns a new array to its argument, and then adds 1 to each element of that new array:

Public Sub ReplaceArray(ByVal A() As Long)

Dim J As Integer

For J = 0 To UBound(A) A(J) = A(J) + 1 Next J End Sub

If ReplaceArray is passed by an array, the value of each element of that array in the called code will not be increased by 1:

ReplaceArray(N)

Instead, after ReplaceArray is called, the element values of the array N remain 10, 20, 30, and 40.

Because N was passed ByVal, ReplaceArray could not modify N by assigning a new array to it. When ReplaceArray created the new array instance K and assigned it to the argument A, then A, which had pointed to the array N in the calling code, instead pointed to the local array K. Consequently, A no longer pointed to the array N in the calling code. Thus, when the members of the array pointed to by A were changed, only the local array K, and not the array N in the calling code, was affected.

A different result would occur if in ReplaceArray the argument were declared ByRef. After ReplaceArray was called, the element values of the array N now would be 11, 21, 31, and 41. Since N was passed ByRef, ReplaceArray could modify N by assigning a new array to it. When ReplaceArray created the new array instance K and assigned it to the argument A, this also had the effect of assigning K to the array variable N in the calling code. Thus, when the members of the array pointed to by A were changed, the array N in the calling code was affected as well as the local array K. Table 9-3 summarizes the effect of passing variables ByVal and ByRef. Table 9-3: Effect of Passing Variables ByVal and ByRef

Variable Type

Passed ByVal

Passed ByRef

Scalar (Integer, Double, and so on.)

Array (contains a pointer to a class or structure instance)

The procedure cannot change the variable.

The procedure can change the values of the individual members of the array to which the variable points, but not the array object to which the variable points.

The procedure can change the variable.

The procedure can change either the array object to which the variable points or the values of the individual members of the array.

Was this article helpful?

0 0

Post a comment