Section 8.6. Passing an Array to a Method


8.6. Passing an Array to a Method

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

  1  ' Fig. 8.11: PassArray.vb  2  ' Passing arrays and individual array elements to methods.  3  Module PassArray  4     Sub Main()  5        Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5}  6  7        Console.WriteLine("EFFECTS OF PASSING AN ENTIRE ARRAY " & _  8           "BY REFERENCE:" & vbCrLf & vbCrLf & _  9           "The values of the original array are:") 10 11        ' display original elements of array1 12        For i As Integer = 0 To array1.GetUpperBound(0) 13           Console.Write(" " & array1(i)) 14        Next 15 16        ModifyArray(array1) ' array is passed by reference 17        Console.WriteLine(vbCrLf & "The values of the modified array are:") 18 19        ' display modified elements of array1 20        For i As Integer = 0 To array1.GetUpperBound(0) 21           Console.Write(" " & array1(i)) 22        Next 23 24        Console.WriteLine(vbCrLf & vbCrLf & _ 25           "EFFECTS OF PASSING AN ARRAY ELEMENT BY VALUE:" & _ 26           vbCrLf & vbCrLf & "array1(3) before ModifyElementByVal: " & _ 27           array1(3)) 28 29        ModifyElementByVal(array1(3)) ' array element passed by value 30        Console.WriteLine("array1(3) after ModifyElementByVal: " & _ 31           array1(3)) 32        Console.WriteLine(vbCrLf & "EFFECTS OF PASSING AN " & _ 33            "ARRAY ELEMENT BY REFERENCE: " & vbCrLf & vbCrLf & _ 34            "array1(3) before ModifyElementByRef: " & array1(3)) 35 36        ModifyElementByRef(array1(3)) ' array element passed by reference 37        Console.WriteLine("array1(3) after ModifyElementByRef: " & _ 38           array1(3)) 39     End Sub ' Main 40 41     ' method modifies array it receives (note ByVal)           42     Sub ModifyArray(ByVal arrayParameter As Integer())         43        For j As Integer = 0 To arrayParameter.GetUpperBound(0) 44           arrayParameter(j) *= 2 ' double the array element    45        Next                                                    46     End Sub ' ModifyArray                                      47 48     ' method modifies integer passed to it                              49     ' original is not modified (note ByVal)                             50     Sub ModifyElementByVal(ByVal element As Integer)                    51        Console.WriteLine("Value received in ModifyElementByVal: " & _   52           element)                                                      53        element *= 2 ' double the array element                          54        Console.WriteLine("Value calculated in ModifyElementByVal: " & _ 55           element)                                                      56     End Sub ' ModifyElementByVal                                        57 58     ' method modifies integer passed to it                              59     ' original is modified (note ByRef)                                 60      Sub ModifyElementByRef(ByRef element As Integer)                   61        Console.WriteLine("Value received in ModifyElementByRef: " & _   62           element)                                                      63        element *= 2 ' double the array element                          64        Console.WriteLine("Value calculated in ModifyElementByRef: " & _ 65           element)                                                      66     End Sub ' ModifyElementByRef                                        67  End Module ' PassArray 

 EFFECTS OF PASSING AN ENTIRE ARRAY BY REFERENCE: The values of the original array are:   1  2  3  4  5 The values of the modified array are:   2  4  6  8  10 EFFECTS OF PASSING AN ARRAY ELEMENT BY VALUE: array1(3) before ModifyElementByVal: 8 Value received in ModifyElementByVal: 8 Value calculated in ModifyElementByVal: 16 array1(3) after ModifyElementByVal: 8 EFFECTS OF PASSING AN ARRAY ELEMENT BY REFERENCE: array1(3) before ModifyElementByRef: 8 Value received in ModifyElementByRef: 8 Value calculated in ModifyElementByRef: 16 array1(3) after ModifyElementByRef: 16 



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

When passing an array to a method, including an empty pair of parentheses after the array name is a syntax error.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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