Re: Passing arrays ByVal vs ByRef
- From: "Cor Ligthert[MVP]" <Notmyfirstname@xxxxxxxxx>
- Date: Sat, 4 Apr 2009 19:59:40 +0200
In the clouds?
Cor
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message news:%23FeyYxUtJHA.5652@xxxxxxxxxxxxxxxxxxxxxxx
No Cor. With ByRef, you don't get a copy of anything, you get a reference to the variable on the stack that points to the object on the heap.
"Cor Ligthert[MVP]" <Notmyfirstname@xxxxxxxxx> wrote in message news:eXaDvLLtJHA.2776@xxxxxxxxxxxxxxxxxxxxxxxByRef references a copy of the value (which is a reference in the case of an array)
"Schizoid Man" <schizoid_man@xxxxxxxxxx> wrote in message news:eGmhn4KtJHA.4140@xxxxxxxxxxxxxxxxxxxxxxx
"Scott M." <s-mar@xxxxxxxxxxxxx> wrote in message news:#T3vJ0JtJHA.4324@xxxxxxxxxxxxxxxxxxxxxxxThis 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
Thanks for the information, Scott. I was misled a bit, because the method I was invoking was declared as:
Public Shared Function(ByVal InputArray() as Double) as Double
So I just assumed that the array that I was passing was using pass-by-value. If I understand correctly, regardless of the usage of ByVal or ByRef, arrays will *always* be pass-by-reference?
Thanks.
.
- Follow-Ups:
- Re: Passing arrays ByVal vs ByRef
- From: Scott M.
- Re: Passing arrays ByVal vs ByRef
- References:
- RE: Passing arrays ByVal vs ByRef
- From: David Anton
- Re: Passing arrays ByVal vs ByRef
- From: Scott M.
- Re: Passing arrays ByVal vs ByRef
- From: Schizoid Man
- Re: Passing arrays ByVal vs ByRef
- From: Cor Ligthert[MVP]
- Re: Passing arrays ByVal vs ByRef
- From: Scott M.
- 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: Re: vb.net interfaces
- Previous by thread: Re: Passing arrays ByVal vs ByRef
- Next by thread: Re: Passing arrays ByVal vs ByRef
- Index(es):
Relevant Pages
|