In VB6, arrays are always passed by Reference. In VB.NET, the default parameter passed by value. But is the ByVal holds true for arrays as well by default??
Thanx ;)
Printable View
In VB6, arrays are always passed by Reference. In VB.NET, the default parameter passed by value. But is the ByVal holds true for arrays as well by default??
Thanx ;)
Yes , all data types in Visual Basic.NET are passed ByVal by default .
There are two types in .NET, value types and reference types.
Value types are the basic types, such as int, long, double, etc. This includes structures and enums. As you would guess, value types are passed by value by default.
Reference types are objects, which is pretty much everything else not mentioned above. This includes arrays and strings. As you would guess, reference types are passed by reference by default.
That's right wyrd:D , referenced type passes a pointer to a memory allocation .
However, the VS.NET IDE automatically puts 'ByVal' in front of every parameter when you write a function, unless you specify otherwise.
In VB6, array was always passed by reference. That mean in VB.NET, we can pass an array both by value & by reference. isn't it??
Thanx
Apparently not.Quote:
Originally posted by sbasak
In VB6, array was always passed by reference. That mean in VB.NET, we can pass an array both by value & by reference. isn't it??
I just found out the hard way that my
is being returned modified from the sub.Code:(ByVal SideLengths() As Single)
That ain't right. DaveBo
Passing an array by value is fairly absurd in theory, since function arguments are generally pushed onto the stack (though maybe not in .NET). DaveBo's cryptic post suggests that arrays are passed by ref as default behavior, which makes sense to be, though it would be inconsistent.
How would you pass an array ByVal? If I really wanted to, I'd make a copy, then pass the reference to the copy as the argument, but .NET will do all that under the covers. Since everything is an object, copying then passing a pointer seems like a likely method of implementing ByVal, and it would work just as well on arrays as intrinsic types.
I was simply passing an array of 3 singles into a sub, I asked it to be passed ByVal and VB never complained so I assumed I was getting what I asked for,
i.e. that VB would make the local array copy for me "under the covers" in the sub.
But my program was failing miserably because VB was lying to me.
I repeat - that ain't right.
I think the point is: You can choose if a valuetype has to be passed byval or byref. A reference type is always passed byref, also if compiler doesn't correct your 'byval'.