O'Reilly logo

VB & VBA in a Nutshell: The Language by Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required