[RESOLVED] ByVal in Function Arguments
If this:
VB Code:
Private Function MyFunc(ByVal sTest As String)
The same as this:
VB Code:
Private Function MyFunc(sTest As String)
Or does the second one mean ByVal/ByRef and require more CPU time? The reason I ask is that I have quite a few arguments in the function I am making and ByVal infront of all of them makes it very long when the AutoFill text pops up.
Re: ByVal in Function Arguments
ByVal means that the function can change the value of the variable. ByRef means that a new instance is invoked, and destroyed when the function ends.
Might take a bit longer to invoke, but you would also save time not having to copy that variable back after the function ends.
Re: ByVal in Function Arguments
Thank dglienna, but I need to know what happens if ByVal and ByRef are ommited. It it treated like ByVal automatically or is it somewhat like variant and take up more CPU time?
Re: ByVal in Function Arguments
dglienna: a bit incorrect. ByVal just means that a copy of the variable is passed to the procedure. ByRef means that the original variable is passed to the procedure. This means that when you change a ByRef variable in the procedure (be it a function or a sub), the passed variable changes as there is no copy of the variable. As far as I know, ByVal is slightly slower than ByRef; I've never tested it really (or if I have, I've forgotten the results long time ago).
I think ByRef was used by default. I'm not sure though, I tend to always use ByRef and ByVal so I can be sure of which it does :D But yes, it is either ByVal or ByRef, nothing other special.
Re: ByVal in Function Arguments
The second example is passing the variable "ByRef" and not ByVal. The default is ByRef when nothing is designated.
Although it may seem easier to not include the ByVal, it actually is a completely different meaning. ByVal is like passing in a
copy of the variable to your function. At the calling line of code the passed value can not be retrieved.
However if you use ByRef then you are only passing a link that represents the variable and any changes to the variable are reflected
back.
Re: ByVal in Function Arguments
byVal is the default in VB.Net
That's a change from VB6 (and prior).
[edit] So specify it (so conversion to .Net is easier)...
Re: ByVal in Function Arguments
Oops. I didn't read it closely enough. The last sentence says can't. Sorry.
Quote:
by value
A way of passing the value, rather than the address, of an argument to a procedure. This allows the procedure to access a copy of the variable. As a result, the variable's actual value can't be changed by the procedure to which it is passed.
Here's byref
Quote:
by reference
A way of passing the address, rather than the value, of an argument to a procedure. This allows the procedure to access the actual variable. As a result, the variable's actual value can be changed by the procedure to which it is passed.