Partager via


How to: Pass Arguments to a Procedure (Visual Basic)

When you call a procedure, you follow the procedure name with an argument list in parentheses. You supply an argument corresponding to every required parameter the procedure defines, and you can optionally supply arguments to the Optional parameters. If you do not supply an Optional parameter in the call, you must include a comma to mark its place in the argument list if you are supplying any subsequent arguments.

If you intend to pass an argument of a data type different from that of its corresponding parameter, such as Byte to String, you can set the type-checking switch (Option Strict Statement) to Off. If Option Strict is On, you must use either widening conversions or explicit conversion keywords. For more information, see Widening and Narrowing Conversions (Visual Basic) and Type Conversion Functions (Visual Basic).

For more information, see Procedure Parameters and Arguments (Visual Basic).

To pass one or more arguments to a procedure

  1. In the calling statement, follow the procedure name with parentheses.

  2. Inside the parentheses, put an argument list. Include an argument for each required parameter the procedure defines, and separate the arguments with commas.

  3. Make sure each argument is a valid expression that evaluates to a data type convertible to the type the procedure defines for the corresponding parameter.

  4. If a parameter is defined as Optional (Visual Basic), you can either include it in the argument list or omit it. If you omit it, the procedure uses the default value defined for that parameter.

  5. If you omit an argument for an Optional parameter and there is another parameter after it in the parameter list, you can mark the place of the omitted argument by an extra comma in the argument list.

    The following example calls the Visual Basic MsgBox function.

    Dim mbResult As MsgBoxResult
    Dim displayString As String = "Show this string to the user"
    mbResult = MsgBox(displayString, , "Put this in the title bar")
    

    The preceding example supplies the required first argument, which is the message string to be displayed. It omits an argument for the optional second parameter, which specifies the buttons to be displayed on the message box. Because the call does not supply a value, MsgBox uses the default value, MsgBoxStyle.OKOnly, which displays only an OK button.

    The second comma in the argument list marks the place of the omitted second argument, and the last string is passed to the optional third parameter of MsgBox, which is the text to be displayed in the title bar.

See Also

Tasks

How to: Define a Parameter for a Procedure (Visual Basic)

Concepts

Sub Procedures (Visual Basic)

Function Procedures (Visual Basic)

Property Procedures (Visual Basic)

Operator Procedures (Visual Basic)

Passing Arguments by Value and by Reference (Visual Basic)

Recursive Procedures (Visual Basic)

Procedure Overloading (Visual Basic)

Object-Oriented Programming (C# and Visual Basic)

Other Resources

Objects and Classes in Visual Basic