This thread become interesting!
Matthew, thanks a lot and congratulations for the observation!
Micron, thanks a lot for the persistence and the research on the subject!
From the useful answer (which until now has get 59 votes) I keep this point:
If you use parentheses in any other context but to enclose procedure call arguments,
VB(A) will attempt to evaluate the expression in the parentheses to a resulting simple value.
That's explains why the calling of a procedure with a single argument enclosed in ps without the Call statement is allowed. msgbox("Hello") doesn't raise an error (single argument) but msgbox("Hello",vbOKOnly) do. ("Hello") = "Hello" but ("Hello",0) drives in Error.
Code:
Sub TestMyMessage()
Dim strIn As String
strIn = "Hello world!"
MsgBox ("MyMessage() Is Type " & TypeName(MyMessage(strIn)))
End Sub
Function MyMessage(ByRef strMsg As String)
MsgBox (strMsg)
End Function
Also, that's explains why VBA pass as ByVal the argument to the calling procedure, because is passing the result of the evaluation and not the pointer to the variable of the caller, and the worst of this fact is that this is applicable to any case of Byref arguments, even in correctly calling procedures with more than one argument.
Try this:
Code:
Sub TestMultiply()
Dim intX As Integer
Dim intY As Integer
intX = 5
intY = 2
Debug.Print "intX before=" & intX
Debug.Print "intY before=" & intY
Call RefMultiply(intX, intY + 2)
Debug.Print "intX after=" & intX
Debug.Print "intY after=" & intY
End Sub
Sub RefMultiply(ByRef x As Integer, ByRef y As Integer)
Debug.Print " RefMultiply: x*y(" & x & "*" & y & ")=" & x * y
x = x * y
End Sub
TestMultiply produce this:
Code:
intX before=5
intY before=2
RefMultiply: x*y(5*4)=20
intX after=20
intY after=2
So, the most important that we have to keep in mind is that, any expression in place of a ByRef argument, puts a new value in place of the pointer and therefor we must to avoid them.
I have to concede that I never realized that until now and I have to say that I learned something today!
Cheers,
John