Re: Passing arrays ByVal vs ByRef

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



This is indeed correct.

When you pass something ByVal, you will get a copy, but most people assume
you get a copy of the data, which is not always the case.

What you get a copy of depends on the type you are passing. If you pass a
reference type (such as an Array), you get a copy of the reference (the
variable pointer), not the type (the Array) and this explains why your
original array is modified when you modify the passed parameter.

When you pass a value type ByVal, you get a copy of the value (the data).

-Scott


"David Anton" <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9516C2EC-94C7-4389-A7EB-653DA2ABF53F@xxxxxxxxxxxxxxxx
You are correct - you would need to make a copy.
VB & C# (or Java) have no concept of marking a parameter as 'unmodifiable
internals'. You can always modify the 'internals' of the object being
passed
- with reference objects, the only thing you can prevent is whether the
top-level reference is modified.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert VB to C#, C++, or Java
Convert C# to VB, C++, or Java
Convert C++ to C#, VB, or Java
Convert Java to C#, C++, or VB


"Schizoid Man" wrote:

Hello,

I have run into a bit of a problem when passing arrays ByVal between
functions - it seems that any modifications to the passed array in the
called method will be reflected in the calling method.

A quick Web search shows that this is, in fact, the expected VB.Net
behavior. What is the way around this - is the correct procedure to make
a
copy of the array in the called method and modify that?

Thanks,
Schiz




.



Relevant Pages

  • Re: Java Generic programming using subclassing
    ... Java nowhere guarantees that the elements of an array ... Now chances are that any Java ... > reference will be visible through the other. ... > eligible for garbage collection, and will in fact be collected before ...
    (comp.lang.java.programmer)
  • Re: no pointer in Java => my problem
    ... > paper and all but ignore the house whose address is on that paper. ... reference; Dale's comments apply instead if you're passing the paper by ... formal parameter of a method even when it is a reference, Java doesn't ... it can't modify the value of any ...
    (comp.lang.java.programmer)
  • Re: no pointer in Java => my problem
    ... > reference; Dale's comments apply instead if you're passing the paper by ... > formal parameter of a method even when it is a reference, Java doesn't ... it can't modify the value of any ...
    (comp.lang.java.programmer)
  • Re: Q: Pass obj by reference, manupulate and access modified obj?
    ... The method can modify the object that is referenced. ... Does Java pass objects by reference or by value? ...
    (comp.lang.java.help)
  • Re: Multidimensional arrays in Java.
    ... created on the stack. ... Java doesn't support multidimensional arrays ... We have to create an array of array to support this ... Here the stud reference variable or object reference is created on the ...
    (comp.lang.java.programmer)