Re: VB-101: Passing Arrays ByVal vs ByRef
- From: John Pass <JohnPass@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 24 May 2005 10:13:02 -0700
Ken, Thanks for your response.
My questions was why the 2nd part of the sub did not change the changes the
output to screen while the 1st part did.
Please see my (longer) resonse to _AnonCoward.
Regards,
John.
"Ken Tucker [MVP]" wrote:
> Hi,
>
> The array is passed byval. Anything you do to the array in the
> subroutine will not be sent back. If you want to be able to make any
> changes to the array pass it byref.
>
> Ken
> --------------------
> "John Pass" <JohnPass@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:F694915A-595C-43B4-B89F-133516ED21F0@xxxxxxxxxxxxxxxx
> 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
>
> *****************************************
>
>
>
.
- References:
- VB-101: Passing Arrays ByVal vs ByRef
- From: John Pass
- Re: VB-101: Passing Arrays ByVal vs ByRef
- From: Ken Tucker [MVP]
- VB-101: Passing Arrays ByVal vs ByRef
- Prev by Date: resolve multiple ip's
- Next by Date: Re: VB-101: Passing Arrays ByVal vs ByRef
- Previous by thread: Re: VB-101: Passing Arrays ByVal vs ByRef
- Next by thread: Re: VB-101: Passing Arrays ByVal vs ByRef
- Index(es):
Relevant Pages
|