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

For example, consider the two routines in the following code that double their parameters:

Sub DoubleItByVal(ByVal X As Single)
    X*= 2
End Sub
 
Sub DoubleItByRef(ByRef X As Single)
    X*= 2
End Sub
 
Sub TestParameters()
    Dim value As Single
             
    value = 10
    DoubleItByVal(value)
    Debug.WriteLine(value)
             
    value = 10
    DoubleItByRef(value)
    Debug.WriteLine(value)
End Sub

Subroutine DoubleItByVal declares its parameter with the ByVal keyword. Behind the scenes, this routine makes a new variable named X and copies the value of its argument into that variable. The parameter X is available within the subroutine. The routine multiplies X by 2 and then exits. At that point, the parameter variable goes out of scope and is destroyed.

Subroutine DoubleItByRef declares its parameter with the ByRef keyword. This routine’s variable X is a reference to the variable passed into the routine. The subroutine doubles X and that doubles the variable in the calling code.

Subroutine TestParameters calls each of these routines. It declares a variable named value, passes it to subroutine DoubleItByVal, and displays the result after DoubleItByVal returns. Because DoubleItByVal declares its parameter ByVal, the variable value is not changed so the result is 10.

Subroutine TestParameters then calls subroutine DoubleItByRef and displays the result after that call returns. Subroutine DoubleItByRef declares its parameter ByRef so the variable value is changed to 20.

Even this more complex view of how procedures handle parameters has exceptions. If you pass a literal value or the result of an expression into a procedure by reference, there is no variable to pass by reference, so Visual Basic creates its own temporary variable. In that case, any changes made to the ByRef parameter are not returned to the calling routine, because that code did not pass a variable into the procedure. The following code shows statements that pass a literal expression and the result of an expression into the DoubleItByRef subroutine:

DoubleItByRef(12)    ' Literal expression.
DoubleItByRef(X + Y) ' Result of an expression.

Another case where a ByRef parameter does not modify a variable in the calling code is when you omit an optional parameter. For example, the following subroutine takes an optional ByRef parameter. If you call this routine and omit the parameter, Visual Basic creates the employee_id parameter from scratch so the subroutine can use it in its calculations. Because you called the routine without passing it a variable, the subroutine does not update a variable.

Sub UpdateEmployee(Optional ByRef employee_id As Integer = 0)
   ...
End Sub

Probably the sneakiest way a ByRef variable can fail to update a variable in the calling routine is if you enclose the variable in parentheses. The parentheses tell Visual Basic to evaluate their contents as an expression, so Visual Basic creates a temporary variable to hold the result of the expression. It then passes the temporary variable into the procedure. If the procedure’s parameter is declared ByRef, it updates the temporary variable, but not the original variable, so the calling routine doesn’t see any change to its value.

The following code calls subroutine DoubleItByRef, passing the variable value into the routine surrounded with parentheses. The DoubleItByRef subroutine doubles the temporary variable Visual Basic creates, leaving value unchanged.

DoubleItByRef((value))

Keep these issues in mind when you work with parameters. Parameters have non-static procedure scope but the ByRef keyword can sometimes carry their values outside of the routine.

For more information on routines and their parameters, see Chapter 16.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.134.154