8.6. Passing an Array to a MethodTo pass an array argument to a method, specify the name of the array without using parentheses. For example, if array hourlyTemperatures has been declared as Dim hourlyTemperatures As Integer() = New Integer(24) {} the method call DayData(hourlyTemperatures) passes array hourlyTemperatures to method DayData. Every array object "knows" its own upper bound (i.e., the value returned by the method GetUpperBound), so when you pass an array object to a method, you do not need to pass the upper bound of the array as a separate argument. For a method to receive an array through a method call, the method's parameter list must specify that an array will be received. For example, the method header for DayData might be written as Sub DayData(ByVal temperatureData As Integer()) indicating that DayData expects to receive an Integer array in parameter temperatureData. In Visual Basic, arrays always are passed by reference, yet it is normally inappropriate to use keyword ByRef in the method definition header. We discuss this subtle (and somewhat complex) issue in more detail in Section 8.14. Although entire arrays are always passed by reference, individual array elements can be passed in the same manner as simple variables of that type. For instance, array element values of primitive types, such as Integer, can be passed either by value or by reference, depending on the method definition. To pass an array element to a method, use the indexed name of the array element as an argument in the method call. The program in Fig. 8.11 demonstrates the difference between passing an entire array and passing an array element. Figure 8.11. Passing arrays and individual array elements to methods.
The For...Next statement in lines 1214 displays the five elements of integer array array1 (line 5). Line 16 passes array1 to method ModifyArray (lines 4246), which then multiplies each element by 2 (line 44). To illustrate that array1's elements were modified in the called method (i.e., as enabled by passing by reference), the For...Next statement in lines 2022 displays the five elements of array1. As the output indicates, the elements of array1 are indeed modified by ModifyArray. To show the value of array1(3) before the call to ModifyElementByVal, lines 2427 display the value of array1(3). Line 29 invokes method ModifyElementByVal (lines 50 56) and passes array1(3). When array1(3) is passed by value, the Integer value in position 3 of array array1 (now an 8) is copied and is passed to method ModifyElementByVal, where it becomes the value of parameter element. Method ModifyElementByVal then multiplies element by 2 (line 53). The parameter element of ModifyElementByVal is a local variable that is destroyed when the method terminates. Thus, when control is returned to Main, the unmodified value of array1(3) is displayed. Lines 3238 demonstrate the effects of method ModifyElementByRef (lines 6066). This method performs the same calculation as ModifyElementByVal, multiplying element by 2. In this case, array1(3) is passed by reference, meaning that the value of array1(3) displayed (lines 3738) is the same as the value calculated in the method (i.e., the original value in the caller is modified by the called method). Common Programming Error 8.4
|