This documentation is archived and is not being maintained.

Parameter Passing Mechanism for Visual Basic 6.0 Users 

Visual Basic 2005 introduces several changes affecting how arguments are passed to procedures.

Default Passing Mechanism

Visual Basic 6.0

In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef. This allows the variable passed into the procedure to be modified in the calling program.

Exposing a variable to modification can lead to a pitfall. In the following example, the passing mechanism defaults to ByRef, the value of elapsedSeconds gets altered by minutesPastHour, and elapsedSeconds is displayed incorrectly by MsgBox.

Function minutesPastHour(seconds As Integer) As Integer 
    Dim hours As Integer = seconds \ 3600 
    seconds = seconds Mod 3600 
    Return seconds \ 60 
End Function 
Sub showSecondsAndMinutes() 
    Dim elapsedSeconds, extraMinutes As Integer 
    elapsedSeconds = CInt(Timer()) ' Integer seconds since midnight. 
    extraMinutes = minutesPastHour(elapsedSeconds) 
    MsgBox "Total seconds: " & elapsedSeconds & _ 
        "; minutes past hour: " & extraMinutes 
End Sub 

Passing an argument ByRef allows a procedure to change it in the calling program, which can lead to unexpected behavior. And if that procedure calls another procedure and passes the same argument ByRef, the chances of unintentionally changing the original variable are increased.

Visual Basic 2005

When you declare a procedure in Visual Basic 2005, the passing mechanism defaults to ByVal for every parameter. This protects arguments against modification. The declaration in the preceding example can be rewritten as follows.

Function MinutesPastHour(ByVal Seconds As Integer) As Integer

Passing seconds by value prevents the procedure from accessing the variable in the calling program, and helps avoid the pitfall just described.

Although ByVal is the default mechanism, specifying it explicitly for every parameter removes uncertainty and makes your code easier to read.

ByRef Property Arguments

Visual Basic 6.0

In Visual Basic 6.0, a property passed to a procedure as a ByRef argument is copied into the procedure but not copied out. This means that any modification to such a property argument is not reflected back to the original property in the calling program, even though it was passed ByRef.

Visual Basic 2005

In Visual Basic 2005, a property argument passed ByRef is copied both into and out of the procedure. The following example demonstrates how a property can be changed by a procedure.

Sub Reduce(ByRef Height As Single)
' ... ... ... ... ... ... ...  ' Code to modify Height argument. 
End Sub

Dim Sq As Square = New Square  ' Square has property Side of type Single. 
Reduce(Sq.Side)                ' Side is changed when Reduce returns. 

When a procedure modifies a property argument, the value of the original property is not changed immediately in the calling program. Instead, it is copied out when the procedure returns.

ParamArray Arguments

Visual Basic 6.0

In Visual Basic 6.0, a procedure can specify the ParamArray keyword on the last of its arguments to accept an array of Variant arguments. You cannot declare the passing mechanism of these arguments. They are always passed ByRef.

Visual Basic 2005

In Visual Basic 2005, ParamArray arguments are always passed ByVal. The arguments in the array must all be of the data type of the ParamArray argument.

See Also

Show: