PARAMETER DECLARATIONS

A parameter declaration for a subroutine, function, or property procedure defines the names and types of the parameters passed into it. Parameter declarations always have non-static procedure scope. Visual Basic creates parameter variables when a procedure begins and destroys them when the procedure ends. The subroutine’s code can access the parameters, but code outside of the routine cannot.

For example, the following subroutine takes an integer as a parameter. The subroutine calls this value employee_id. Code within the subroutine can access employee_id and code outside of the subroutine cannot.

Public Sub DisplayEmployee(ByVal employee_id As Integer)
    ...
End Sub

A parameter’s basic scope is straightforward (non-static procedure scope), but parameters have some special features that complicate the situation. Although this isn’t exactly a scoping issue, it’s related closely enough to scope that it’s worth covering here.

You can declare a parameter ByRef or ByVal (ByVal is the default if you use neither keyword). If you declare the variable ByVal, which stands for “by value,” the routine makes its own local parameter variable with procedure scope just as you would expect.

If you declare a parameter with the keyword ByRef, which stands for “by reference,” the routine does not create a separate copy of the parameter variable. Instead, it uses a reference to the parameter you pass in, and any changes the routine makes to the value are reflected in the calling ...

Get Visual Basic 2012 Programmer's Reference 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.