Download presentation
Presentation is loading. Please wait.
1
Passing Arrays to Procedures: ByVal vs. ByRef
2
Passing Arrays to Procedures Passing the Array – Specify the name of the array without using parentheses – Every array object “knows” its own upper bound Do not need to pass the upper bound of the array as a separate argument Dim hourlyTemperatures As Integer() = New Integer(24) {} DayData(hourlyTemperatures) In Visual Basic, arrays always are passed by reference Receiving the array – The procedure’s parameter list must specify that an array will be received. Sub DayData(ByVal temperatureData As Integer())
3
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 data types, such as Integer, can be passed by value or by reference, depending on the procedure definition. To pass an array element to a procedure, use the indexed name of the array element as an argument in the call to the procedure. The following program demonstrates the difference between passing an entire array and passing an array element. Passing Arrays to Procedures
4
1 ' Fig. 7.8: PassArray.vb 2 ' Passing arrays and individual array elements to procedures. 3 4 Imports System.Windows.Forms 5 6 Module modPassArray 7 Dim output As String 8 9 Sub Main() 10 Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5} 11 Dim i As Integer 12 13 output = "EFFECTS OF PASSING ENTIRE ARRAY " & _ 14 "BY REFERENCE:" & vbCrLf & vbCrLf & _ 15 "The values of the original array are:" & vbCrLf 16 17 ' display original elements of array1 18 For i = 0 To array1.GetUpperBound(0) 19 output &= " " & array1(i) 20 Next 21 22 ModifyArray(array1) ' array is passed by reference 23
5
24 output &= vbCrLf & _ 25 "The values of the modified array are:" & vbCrLf 26 27 ' display modified elements of array1 28 For i = 0 To array1.GetUpperBound(0) 29 output &= " " & array1(i) 30 Next 31 32 output &= vbCrLf & vbCrLf & _ 33 "EFFECTS OF PASSING ARRAY ELEMENT " & _ 34 "BY VALUE:" & vbCrLf & vbCrLf & "array1(3) " & _ 35 "before ModifyElementByVal: " & array1(3) 36 37 ' array element passed by value 38 ModifyElementByVal(array1(3)) 39 40 output &= vbCrLf & "array1(3) after " & _ 41 "ModifyElementByVal: " & array1(3) 42 43 output &= vbCrLf & vbCrLf & "EFFECTS OF PASSING " & _ 44 "ARRAY ELEMENT BY REFERENCE: " & vbCrLf & vbCrLf & _ 45 "array1(3) before ModifyElementByRef: " & array1(3) 46
6
47 ' array element passed by reference 48 ModifyElementByRef(array1(3)) 49 50 output &= vbCrLf & "array1(3) after " & _ 51 "ModifyElementByRef: " & array1(3) 52 53 MessageBox.Show(output, "Passing Arrays", _ 54 MessageBoxButtons.OK, MessageBoxIcon.Information) 55 End Sub ' Main 56 57 ' procedure modifies array it receives (note ByVal) 58 Sub ModifyArray(ByVal arrayParameter As Integer()) 59 Dim j As Integer 60 61 For j = 0 To arrayParameter.GetUpperBound(0) 62 arrayParameter(j) *= 2 63 Next 64 65 End Sub ' ModifyArray 66
7
67 ' procedure modifies integer passed to it 68 ' original is not be modified (note ByVal) 69 Sub ModifyElementByVal(ByVal element As Integer) 70 71 output &= vbCrLf & "Value received in " & _ 72 "ModifyElementByVal: " & element 73 element *= 2 74 output &= vbCrLf & "Value calculated in " & _ 75 "ModifyElementByVal: " & element 76 End Sub ' ModifyElementByVal 77 78 ' procedure modifies integer passed to it 79 ' original is be modified (note ByRef) 80 Sub ModifyElementByRef(ByRef element As Integer) 81 82 output &= vbCrLf & "Value received in " & _ 83 "ModifyElementByRef: " & element 84 element *= 2 85 output &= vbCrLf & "Value calculated in " & _ 86 "ModifyElementByRef: " & element 87 End Sub ' ModifyElementByRef 88 89 End Module ' modPassArray
9
Passing Arrays: ByVal vs. ByRef ByVal ByVal – Causes the value of the argument to be copied to a local variable in the procedure – Changes to the local variable are reflected in the local copy of that variable, not in the original variable in the calling program – But if the argument is of a reference type, like an array, passing it ByVal actually passes it by reference, so changes to the object affect the original objects in the callers
10
1 ' Fig. 7.9: ArrayReferenceTest.vb 2 ' Testing the effects of passing array references using 3 ' ByVal and ByRef. 4 5 Module modArrayReferenceTest 6 7 Sub Main() 8 Dim i As Integer 9 10 ' declare array references 11 Dim firstArray As Integer() 12 Dim firstArrayCopy As Integer() 13 14 ' allocate firstArray and copy its reference 15 firstArray = New Integer() {1, 2, 3} 16 firstArrayCopy = firstArray 17 18 Console.WriteLine("Test passing array reference " & _ 19 "using ByVal.") 20 Console.Write("Contents of firstArray before " & _ 21 "calling FirstDouble: ") 22 23 ' print contents of firstArray 24 For i = 0 To firstArray.GetUpperBound(0) 25 Console.Write(firstArray(i) & " ") 26 Next Prints contents first to verify that FirstDouble indeed changes the array’s contents Copies reference firstArray to variable firstArrayCopy, now they reference the same object Demonstrates the subtle difference between passing a reference ByVal vs. passing a reference ByRef.
11
27 28 ' pass firstArray using ByVal 29 FirstDouble(firstArray) 30 31 Console.Write(vbCrLf & "Contents of firstArray after " & _ 32 "calling FirstDouble: ") 33 34 ' print contents of firstArray 35 For i = 0 To firstArray.GetUpperBound(0) 36 Console.Write(firstArray(i) & " ") 37 Next 38 39 ' test whether reference was changed by FirstDouble 40 If firstArray Is firstArrayCopy Then 41 Console.WriteLine(vbCrLf & "The references are " & _ 42 "equal.") 43 Else 44 Console.WriteLine(vbCrLf & "The references are " & _ 45 "not equal.") 46 End If 47 48 ' declare array references 49 Dim secondArray As Integer() 50 Dim secondArrayCopy As Integer() 51 Compares references firstArray and firstArrayCopy VB provides operator Is for comparing references to determine whether they are referencing the same object. firstArray is passed to FirstDouble Reference is passed ByVal
12
52 ' allocate secondArray and copy its reference 53 secondArray = New Integer() {1, 2, 3} 54 secondArrayCopy = secondArray 55 56 Console.WriteLine(vbCrLf & "Test passing array " & _ 57 "reference using ByRef.") 58 Console.Write("Contents of secondArray before " & _ 59 "calling SecondDouble: ") 60 61 ' print contents of secondArray before procedure call 62 For i = 0 To secondArray.GetUpperBound(0) 63 Console.Write(secondArray(i) & " ") 64 Next 65 66 ' pass secondArray using ByRef 67 SecondDouble(secondArray) 68 69 Console.Write(vbCrLf & "Contents of secondArray " & _ 70 "after calling SecondDouble: ") 71 72 ' print contents of secondArray after procedure call 73 For i = 0 To secondArray.GetUpperBound(0) 74 Console.Write(secondArray(i) & " ") 75 Next 76
13
77 ' test whether the reference was changed by SecondDouble 78 If secondArray Is secondArrayCopy Then 79 Console.WriteLine(vbCrLf & "The references are " & _ 80 "equal.") 81 Else 82 Console.WriteLine(vbCrLf & "The references are " & _ 83 "not equal.") 84 End If 85 86 End Sub ' Main 87 88 ' procedure modifies elements of array and assigns 89 ' new reference (note ByVal) 90 Sub FirstDouble(ByVal array As Integer()) 91 Dim i As Integer 92 93 ' double each element value 94 For i = 0 To array.GetUpperBound(0) 95 array(i) *= 2 96 Next 97 98 ' create new reference and assign it to array 99 array = New Integer() {11, 12, 13} 100 End Sub ' FirstDouble Allocates a new array, and attempts to assign it’s reference to parameter array, attempting to overwrite reference firstArray in memory, but will fail because the reference was passed ByVal Multiplies all the elements of the array by 2
14
101 102 ' procedure modifies elements of array and assigns 103 ' new reference (note ByRef) 104 Sub SecondDouble(ByRef array As Integer()) 105 Dim i As Integer 106 107 ' double contents of array 108 For i = 0 To array.GetUpperBound(0) 109 array(i) *= 2 110 Next 111 112 ' create new reference and assign it to array 113 array = New Integer() {11, 12, 13} 114 End Sub ' SecondDouble 115 116 End Module ' modPassArray Because the reference was passed with ByRef, the called procedure has the ability to modify what the reference actually points to
15
Test passing array reference using ByVal. Contents of firstArray before calling FirstDouble: 1 2 3 Contents of firstArray after calling FirstDouble: 2 4 6 The references are equal. Test passing array reference using ByRef. Contents of secondArray before calling SecondDouble: 1 2 3 Contents of secondArray after calling SecondDouble: 11 12 13 The references are not equal.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.