Re: VB-101: Passing Arrays ByVal vs ByRef



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



Relevant Pages

  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changed if an array is passed by Val. ... ' new reference ... As each function creates a new array object, ... 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... if passed ByVal, does not change the original array data in memory, because ... "an array object is created on the heap with two references". ... SecondDoublewould be a reference to firstArray and not a new object. ... > 'secondArray' and 'secondArrayCopy'. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... My real questions was why the 2nd part of the sub did not change the changes ... > change except any change to the actual array object pointer *is* sent back. ... >> Dim firstArray As Integer ... >> ' allocate secondArray and copy its reference ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to pass function name as a parameter?
    ... > Public Sub operator_index(ByVal index As Integer, ... I'm not sure what you mean by return type of reference, ... want to do is have an object that acts like an array, ... overlook what you can do with Properties, which don't really have a C++ ...
    (microsoft.public.vb.syntax)
  • Re: newbie baffled by de/referencing with subroutines
    ... > and I want to use data from that array after the sub is finished. ... > then tried to print the data outside the sub by this dereference: ... > What's wrong with my reference and/or dereference, ...
    (comp.lang.perl.misc)