Reputation: 27
I'm really confused on when a ByRef and a ByVal should be used. I've been told that a Function should always return a value where as a Sub doesn't have to/can return multiple values. However, I'm really stuck on when I should use ByRef and ByVal.
Function Check(ByRef valid As Boolean, ByVal prize As Integer) As Boolean
If prize < 1000000 Or prize > 4000000 Then valid = False
Return valid
End Function
For example, above, I need valid to be changed by the Function, but if I used ByVal, it wouldn't change it as ByVal's don't keep the change of the value and pass it back. Please help, tyvm.
Upvotes: 0
Views: 2109
Reputation: 13571
‘ByRef’ does one thing: it modifies the local variable or property that is passed in as an argument. If you don’t want to do that, and you seldom do, it shouldn’t be used.
In your example, it might be possible to come up with a scenario where that code makes some sort of sense, but it would never be really clear. It both modifies the value of a variable in the calling method, and returns the possibily modified value.
The two use cases for ‘ByRef’ are conditional initialization and multiple return. You code sorta does both. But it does so in a way that is unusual and not really useful. Typically the initializing value comes from within the method, in this case the initializing has to happen first.
The core of the function is a check to see if the price is within a certain range, but because ‘valid’ may already be set to false, even if the value of ‘price’ is outside the unacceptable range, it may still return false.
Dim valid As Boolean = true
...
valid=valid AndAlso Check(price)
Would do the same thing and be a lot easier to understand.
Upvotes: 0
Reputation: 54457
It is correct to say that a Function
always returns a value (even if that value is Nothing
) and a Sub
does not. In C#, all methods are functions and the C# equivalent to a VB Sub
is a function with a return type of void
The default for parameters in VB is ByVal
and you should use that almost every time. It is rare that you need to use ByRef
. Below are some good guidelines to follow:
Sub
.Function
that returns that output.Integer.TryParse
always produces a Boolean
output and also produces an Integer
output if and only if the Boolean
is True
, thus the Boolean
is the natural primary output.Function
that returns the primary output and uses ByRef
parameters for the rest.Sub
that uses ByRef
parameters for all outputs.Note that the only circumstances under which you need to declare a parameter ByRef
are as follows:
There's no other reason to declare a parameter ByRef
. People coming from VB6 often think that they need to declare reference type parameters ByRef
to avoid copying large objects but that is not the case. A reference type variable already contains a reference to an object rather than an object itself. Passing a parameter by value copies a variable so if a variable contains a reference, copying it is copying a reference to an object and not the object.
Upvotes: 3