Re: VB-101: Passing Arrays ByVal vs ByRef



David, Thanks for your response.
My real questions was why the 2nd part of the sub did not change the changes
the output to screen while the 1st part did.
If every change is sent back, why not the changes made by the 2nd part of
the sub.
Please see my (longer) response to _AnonCoward.

Regards,

John

"David Anton" wrote:

> Passing an array byval is exactly like passing any other object byval - any
> change to the object internals is seen in the calling code. So *every*
> change except any change to the actual array object pointer *is* sent back.
>
> David Anton
> www.tangiblesoftwaresolutions.com
> Home of the Instant C# VB.NET to C# converter
> and the Instant VB C# to VB.NET converter
>
> "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
    ... My questions was why the 2nd part of the sub did not change the changes the ... > changes to the array pass it byref. ... > ' new reference ... > ' allocate secondArray and copy its reference ...
    (microsoft.public.dotnet.languages.vb)
  • VB-101: Passing Arrays ByVal vs ByRef
    ... if an array is passed by Val. ... ' new reference ... Dim firstArray As Integer ... Dim secondArray As Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... changes to the array pass it byref. ... ' new reference ... Dim firstArray As Integer ... Dim secondArray As Integer ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VB-101: Passing Arrays ByVal vs ByRef
    ... Passing an array byval is exactly like passing any other object byval - any ... > ' new reference ... > ' allocate firstArray and copy its reference ... > ' allocate secondArray and copy its reference ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Updated datestamp doesnt work
    ... Public Sub StoreMyOldVals ... ' store values of current row in array ... Dim dbs As DAO.Database, rst As DAO.Recordset ... Dim var As Variant ...
    (microsoft.public.access.gettingstarted)

Loading