G
Guest
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line number) which is the last line of the
following sub routine:
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
' double each element value
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' FirstDouble
Since the line where the values of the array 'array' are filled by the
values 11, 12 and 13 is the later part of the same sub-routine where the
earlier changes of values in the same array took place, I would expect these
values to be printed to the screen as output. To me, it looks like the array
values being returned to the calling function were first doubled and that
simply overwritten. Apparently this does not happen.
Can anyone explain why this happens?
(The complete example is attached below
Thanks for your help,
John
*********************************
Module modArrayReferenceTest
Sub Main()
Dim i As Integer
' declare array references
Dim firstArray As Integer()
Dim firstArrayCopy As Integer()
' allocate firstArray and copy its reference
firstArray = New Integer() {1, 2, 3}
firstArrayCopy = firstArray
Console.WriteLine("Test passing array reference " & _
"using ByVal.")
Console.Write("Contents of firstArray before " & _
"calling FirstDouble: ")
' print contents of firstArray
For i = 0 To firstArray.GetUpperBound(0)
Console.Write(firstArray(i) & " ")
Next
' pass firstArray using ByVal
FirstDouble(firstArray)
Console.Write(vbCrLf & "Contents of firstArray after " & _
"calling FirstDouble: ")
' print contents of firstArray
For i = 0 To firstArray.GetUpperBound(0)
Console.Write(firstArray(i) & " ")
Next
' test whether reference was changed by FirstDouble
If firstArray Is firstArrayCopy Then
Console.WriteLine(vbCrLf & "The references are " & _
"equal.")
Else
Console.WriteLine(vbCrLf & "The references are " & _
"not equal.")
End If
' declare array references
Dim secondArray As Integer()
Dim secondArrayCopy As Integer()
' allocate secondArray and copy its reference
secondArray = New Integer() {1, 2, 3}
secondArrayCopy = secondArray
Console.WriteLine(vbCrLf & "Test passing array " & _
"reference using ByRef.")
Console.Write("Contents of secondArray before " & _
"calling SecondDouble: ")
' print contents of secondArray before procedure call
For i = 0 To secondArray.GetUpperBound(0)
Console.Write(secondArray(i) & " ")
Next
' pass secondArray using ByRef
SecondDouble(secondArray)
Console.Write(vbCrLf & "Contents of secondArray " & _
"after calling SecondDouble: ")
' print contents of secondArray after procedure call
For i = 0 To secondArray.GetUpperBound(0)
Console.Write(secondArray(i) & " ")
Next
' test whether the reference was changed by SecondDouble
If secondArray Is secondArrayCopy Then
Console.WriteLine(vbCrLf & "The references are " & _
"equal.")
Else
Console.WriteLine(vbCrLf & "The references are " & _
"not equal.")
End If
End Sub ' Main
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
' double each element value
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' FirstDouble
' procedure modifies elements of array and assigns
' new reference (note ByRef)
Sub SecondDouble(ByRef array As Integer())
Dim i As Integer
' double contents of array
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' SecondDouble
End Module ' modPassArray
*****************************************
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line number) which is the last line of the
following sub routine:
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
' double each element value
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' FirstDouble
Since the line where the values of the array 'array' are filled by the
values 11, 12 and 13 is the later part of the same sub-routine where the
earlier changes of values in the same array took place, I would expect these
values to be printed to the screen as output. To me, it looks like the array
values being returned to the calling function were first doubled and that
simply overwritten. Apparently this does not happen.
Can anyone explain why this happens?
(The complete example is attached below
Thanks for your help,
John
*********************************
Module modArrayReferenceTest
Sub Main()
Dim i As Integer
' declare array references
Dim firstArray As Integer()
Dim firstArrayCopy As Integer()
' allocate firstArray and copy its reference
firstArray = New Integer() {1, 2, 3}
firstArrayCopy = firstArray
Console.WriteLine("Test passing array reference " & _
"using ByVal.")
Console.Write("Contents of firstArray before " & _
"calling FirstDouble: ")
' print contents of firstArray
For i = 0 To firstArray.GetUpperBound(0)
Console.Write(firstArray(i) & " ")
Next
' pass firstArray using ByVal
FirstDouble(firstArray)
Console.Write(vbCrLf & "Contents of firstArray after " & _
"calling FirstDouble: ")
' print contents of firstArray
For i = 0 To firstArray.GetUpperBound(0)
Console.Write(firstArray(i) & " ")
Next
' test whether reference was changed by FirstDouble
If firstArray Is firstArrayCopy Then
Console.WriteLine(vbCrLf & "The references are " & _
"equal.")
Else
Console.WriteLine(vbCrLf & "The references are " & _
"not equal.")
End If
' declare array references
Dim secondArray As Integer()
Dim secondArrayCopy As Integer()
' allocate secondArray and copy its reference
secondArray = New Integer() {1, 2, 3}
secondArrayCopy = secondArray
Console.WriteLine(vbCrLf & "Test passing array " & _
"reference using ByRef.")
Console.Write("Contents of secondArray before " & _
"calling SecondDouble: ")
' print contents of secondArray before procedure call
For i = 0 To secondArray.GetUpperBound(0)
Console.Write(secondArray(i) & " ")
Next
' pass secondArray using ByRef
SecondDouble(secondArray)
Console.Write(vbCrLf & "Contents of secondArray " & _
"after calling SecondDouble: ")
' print contents of secondArray after procedure call
For i = 0 To secondArray.GetUpperBound(0)
Console.Write(secondArray(i) & " ")
Next
' test whether the reference was changed by SecondDouble
If secondArray Is secondArrayCopy Then
Console.WriteLine(vbCrLf & "The references are " & _
"equal.")
Else
Console.WriteLine(vbCrLf & "The references are " & _
"not equal.")
End If
End Sub ' Main
' procedure modifies elements of array and assigns
' new reference (note ByVal)
Sub FirstDouble(ByVal array As Integer())
Dim i As Integer
' double each element value
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' FirstDouble
' procedure modifies elements of array and assigns
' new reference (note ByRef)
Sub SecondDouble(ByRef array As Integer())
Dim i As Integer
' double contents of array
For i = 0 To array.GetUpperBound(0)
array(i) *= 2
Next
' create new reference and assign it to array
array = New Integer() {11, 12, 13}
End Sub ' SecondDouble
End Module ' modPassArray
*****************************************