Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Parameter Passing Mechanism Changes in Visual Basic

Visual Basic .NET introduces several changes affecting the mechanism of passing arguments 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 argument, 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 
   MinutesPastHour = Seconds \ 60 
End Function 
' ... 
ElapsedSeconds = CInt(Timer( ))  ' Integer seconds since midnight. 
ExtraMinutes = MinutesPastHour(ElapsedSeconds) 
MsgBox "Total seconds: " & ElapsedSeconds & _ 
       "; minutes past hour: " & ExtraMinutes 

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

When you declare a procedure in Visual Basic .NET, the passing mechanism defaults to ByVal for every argument. 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 argument 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 .NET

In Visual Basic .NET, 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 .NET

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

See Also

Procedure Declaration Changes in Visual Basic | Procedure Calling Sequence Changes in Visual Basic | Parameter Arrays | Function Statement | Sub Statement | ByVal | ByRef | ParamArray | Programming Element Support Changes Summary

© 2015 Microsoft