Parameter Declarations


Parameter declarations for subroutines, functions, and property procedures always have nonstatic 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, while code outside of the subroutine cannot.

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

Whereas a parameter’s basic scope is straightforward (nonstatic procedure scope), 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, the routine makes its own local parameter variable with procedure scope just as you would expect.

If you declare a parameter with the ByRef keyword, 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, the following code includes two routines that double their parameters. Subroutine DoubleItByVal declares its parameter with the ByVal keyword. This routine makes a new variable named X and copies the value of its parameter into that variable. The parameter X is available within the subroutine, the routine multiplies it 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 the call returns. Subroutine DoubleItByRef declares its parameter ByRef so the variable value is changed to 20.

  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 

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, there is no variable to pass by reference, so Visual Basic must create its own temporary variable. In that case, any changes made to a 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 variable. 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 nonstatic 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 6.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net