-
Apr 4th, 2003, 05:01 AM
#1
Thread Starter
Fanatic Member
Array ByVal or ByRef in VB.NET?
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
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
-
Apr 4th, 2003, 06:06 AM
#2
Sleep mode
Yes , all data types in Visual Basic.NET are passed ByVal by default .
-
Apr 4th, 2003, 10:46 PM
#3
Member
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.
-
Apr 5th, 2003, 03:34 AM
#4
Sleep mode
That's right wyrd , referenced type passes a pointer to a memory allocation .
-
Apr 5th, 2003, 09:19 PM
#5
Hyperactive Member
However, the VS.NET IDE automatically puts 'ByVal' in front of every parameter when you write a function, unless you specify otherwise.
-
Apr 7th, 2003, 04:57 AM
#6
Thread Starter
Fanatic Member
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
Life is a one way journey, not a destination. Travel it with a smile and never regret anything.
Yesterday is history, tomorrow is a mystery, today is gift - that's why we call it present.
-
Nov 8th, 2004, 02:27 PM
#7
Hyperactive Member
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??
Apparently not.
I just found out the hard way that my
Code:
(ByVal SideLengths() As Single)
is being returned modified from the sub.
That ain't right. DaveBo
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
Nov 8th, 2004, 02:44 PM
#8
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.
My usual boring signature: Nothing
-
Nov 8th, 2004, 03:15 PM
#9
Hyperactive Member
re. my cryptic post
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.
"The wise man doesn't know all the answers, but he knows where to find them."
VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15
-
Nov 9th, 2004, 03:27 AM
#10
Hyperactive Member
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'.
Live long and prosper (Mr. Spock)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|