Udostępnij za pośrednictwem


How to: Call a Procedure that Does Not Return a Value

A Sub procedure does not return a value to the calling code. You call it explicitly with a stand-alone calling statement. You cannot call it by simply using its name within an expression.

The calling statement can optionally use the Call keyword. This is recommended, because it improves the readability of your code.

To call a Sub procedure

  1. Use a Call statement, following the Call keyword with the name of the Sub procedure.

  2. Follow the procedure name with parentheses to enclose the argument list. If there are no arguments, you can optionally omit the parentheses. However, using the parentheses makes your code easier to read.

  3. Place the arguments in the argument list within the parentheses, separated by commas. Be sure you supply the arguments in the same order that the Sub procedure defines the corresponding parameters.

    The following example calls the Visual Basic AppActivate function to activate an application window. AppActivate takes the window title as its sole argument. It does not return a value to the calling code. If a Notepad process is not running, the example throws an ArgumentException. The Shell procedure assumes the applications are in the paths specified.

    Dim notepadID As Integer 
    ' Activate a running Notepad process.
    AppActivate("Untitled - Notepad") 
    ' AppActivate can also use the return value of the Shell function. 
    ' Shell runs a new instance of Notepad.
    notepadID = Shell("C:\WINNT\NOTEPAD.EXE", AppWinStyle.NormalFocus)
    ' Activate the new instance of Notepad.  
    AppActivate(notepadID)
    

See Also

Tasks

How to: Create a Procedure

How to: Call a Procedure that Returns a Value

How to: Call an Event Handler in Visual Basic

Concepts

Procedures in Visual Basic

Sub Procedures

Procedure Parameters and Arguments

Reference

Sub Statement (Visual Basic)

Shell Function

ArgumentException