Re: Passing arrays ByVal vs ByRef
- From: "Scott M." <s-mar@xxxxxxxxxxxxx>
- Date: Fri, 3 Apr 2009 16:49:10 -0400
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
.
- Follow-Ups:
- Re: Passing arrays ByVal vs ByRef
- From: Schizoid Man
- Re: Passing arrays ByVal vs ByRef
- References:
- RE: Passing arrays ByVal vs ByRef
- From: David Anton
- RE: Passing arrays ByVal vs ByRef
- Prev by Date: Re: VB[2008] Newbie form question - removing standard menu when clicking on form icons
- Next by Date: How to add a custom form to a standard app.
- Previous by thread: RE: Passing arrays ByVal vs ByRef
- Next by thread: Re: Passing arrays ByVal vs ByRef
- Index(es):
Relevant Pages
|