ByVal | Optional. Indicates that the argument is passed by value. |
ByRef | Optional. Indicates that the argument is passed by reference. ByRef is the default in Visual Basic. |
I generally use neither ByVal nor ByRef.
I most commonly use ByVal when I am testing an argument and returning a status. For example “Return TRUE if this string contains this sub-string”. I understand that
I could as easily enclose the argument in parentheses to force an interim value to be passed to the procedure.
For the life of me I can think of only one reason for using ByRef, and that is for purposes of documentation. Declaring an argument as ByRef seems to emphasize to the reader that I may well be modifying the argument for the sake of the calling procedure.
Thus a procedure FindNextString(strSourceString, ByRef lngFoundPointer) makes it quite clear that the calling procedure plans to make use of a possibly-changed value in the Long argument.
Can anyone suggest a mechanical/programming reason for declaring ByRef?
Thanks
Chris