3.9. Passing Parameters

There are numerous occasions when you need to call a custom function or subroutine from another function or subroutine, and a variable you are using in the calling procedure is needed in the called procedure. You therefore pass the variable as a parameter to the called procedure. Whether the called procedure is in the same module, the same project, or is a method within a class on a remote server, passing variables from one procedure to the other is always the same.

The called procedure, and not the calling procedure, determines how the variable is passed from the calling to the called procedure. As the user of a called procedure, you have no control over how Visual Basic treats the passed parameters. As the author of a called procedure, it's up to you to decide how best to bring in variables from calling procedures.

Visual Basic allows you to pass variables between procedures and components in two ways. Within the function or subroutine definition, you specify either ByRef or ByVal for each of the variables in the argument list.

3.9.1. ByRef

This is the default method for passing variables between procedures in Visual Basic; that is, if you specify neither ByVal nor ByRef, VB treats the variable as though it had been specified as ByRef.

ByRef means that the variable is passed by reference. In other words, only a reference to the original variable is passed to the called procedure. The called procedure doesn't get its own copy of the variable; it ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.