How to: Define a Parameter for a Procedure (Visual Basic)
A parameter allows the calling code to pass a value to the procedure when it calls it. You declare each parameter for a procedure the same way you declare a variable, specifying its name and data type. You also specify the passing mechanism, and whether the parameter is optional.
For more information, see Procedure Parameters and Arguments.
To define a procedure parameter
In the procedure declaration, add the parameter name to the procedure's parameter list, separating it from other parameters by commas.
Decide the data type of the parameter.
Follow the parameter name with an
Asclause to specify the data type.
Decide the passing mechanism you want for the parameter. Normally you pass a parameter by value, unless you want the procedure to be able to change its value in the calling code.
Precede the parameter name with ByVal or ByRef to specify the passing mechanism. For more information, see Differences Between Passing an Argument By Value and By Reference.
If the parameter is optional, precede the passing mechanism with Optional and follow the parameter data type with an equal sign (
=) and a default value.
The following example defines the outline of a
Subprocedure with three parameters. The first two are required and the third is optional. The parameter declarations are separated in the parameter list by commas.
Sub updateCustomer(ByRef c As customer, ByVal region As String, Optional ByVal level As Integer = 0) ' Insert code to update a customer object. End Sub
The first parameter accepts a
updateCustomercan directly update the variable passed to
cbecause the argument is passed ByRef. The procedure cannot change the values of the last two arguments because they are passed ByVal.
If the calling code does not supply a value for the
levelparameter, Visual Basic sets it to the default value of 0.
If the type checking switch (Option Strict Statement) is
Asclause is optional when you define a parameter. However, if any one parameter uses an
Asclause, all of them must use it. If the type checking switch is
Asclause is required for every parameter definition.
Specifying data types for all your programming elements is known as strong typing. When you set
Option Strict On, Visual Basic enforces strong typing. This is strongly recommended, for the following reasons:
It enables IntelliSense support for your variables and parameters. This allows you to see their properties and other members as you type in your code.
It allows the compiler to perform type checking. This helps catch statements that can fail at run time due to errors such as overflow. It also catches calls to methods on objects that do not support them.
It results in faster execution of your code. One reason for this is that if you do not specify a data type for a programming element, the Visual Basic compiler assigns it the
Objecttype. Your compiled code might have to convert back and forth between
Objectand other data types, which reduces performance.