Re: VB-101: Passing Arrays ByVal vs ByRef

Tech-Archive recommends: Fix windows errors by optimizing your registry



"John Pass" <JohnPass@xxxxxxxxxxxxxxxxxxxxxxxxx> schrieb:
You write: 'assigning a new array object to the paramater
won't change the reference of the variable being passed to the method'.

Do you mean that for the code section of line 99? :
'array = New Integer() {11, 12, 13}'
That would mean that a new object is created rather than the old one's data
are overwritten.

Yes. You are creating a new array and assign it to the method's parameter. If the parameter is passed 'ByVal', the array passed to the method in the parameter is not changed, because you are assigning the new array to a temporary copy of the array pointer passed to the method.


\\\
Public Sub Foo1(ByVal Bla() As Integer)
   Bla = New Integer() {10, 20}
End Sub

Public Sub Foo2(ByRef Bla() As Integer)
   Bla = New Integer() {10, 20}
End Sub
..
..
..
Dim a() As Integer = {1, 2}
Foo1(a)

' 'a' still points to the array containing {1, 2}.

Foo2(a)

' 'a' points to the array containing {10, 20}.
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


.



Relevant Pages

  • Re: Large Matrix Handling Issue 64 bit
    ... why 8GB is the practical limit to array size. ... the available largest memory chunk. ... bla, bla, ...
    (comp.soft-sys.matlab)
  • Re: Newbie-Question: Function-Parameters
    ... > What is the most common way to pass an array into a function - and how ... By pointer, which is even simpler than you may think. ... void foobar(int bla[]) ... void foobar(int* bla) ...
    (comp.lang.c)
  • Re: NEw to Perl
    ... > bla bla ... I want while to read the sub list into an array.. ... Fort Worth, Texas ...
    (comp.lang.perl.misc)
  • Re: Using an array of bytes with fixedoffset in a struct?
    ... Public Sub Initialize() ... word array, and one as a byte array, for use in generating CRCs etc) ... reference type, but that's not going to work well for me seeing as ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Hide contents in source code
    ... You'd probably want to put that code into a separate assembly and then obfuscate that DLL. ... The code behind would reference that assembly and call its custom method that you created. ... the code is visible(all other button events, bla bla). ...
    (microsoft.public.dotnet.framework.aspnet)