Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Sub Expression (Visual Basic)

Declares the parameters and code that define a subroutine lambda expression.

Sub ( [ parameterlist ] ) statement
- or -
Sub ( [ parameterlist ] )
  [ statements ]
End Sub

Term

Definition

parameterlist

Optional. A list of local variable names that represent the parameters of the procedure. The parentheses must be present even when the list is empty. For more information, see Parameter List (Visual Basic).

statement

Required. A single statement.

statements

Required. A list of statements.

A lambda expression is a subroutine that does not have a name and that executes one or more statements. You can use a lambda expression anywhere that you can use a delegate type, except as an argument to RemoveHandler. For more information about delegates, and the use of lambda expressions with delegates, see Delegate Statement and Relaxed Delegate Conversion (Visual Basic).

The syntax of a lambda expression resembles that of a standard subroutine. The differences are as follows:

  • A lambda expression does not have a name.

  • A lambda expression cannot have a modifier, such as Overloads or Overrides.

  • The body of a single-line lambda expression must be a statement, not an expression. The body can consist of a call to a sub procedure, but not a call to a function procedure.

  • In a lambda expression, either all parameters must have specified data types or all parameters must be inferred.

  • Optional and ParamArray parameters are not permitted in lambda expressions.

  • Generic parameters are not permitted in lambda expressions.

Following is an example of a lambda expression that writes a value to the console. The example shows both the single-line and multiline lambda expression syntax for a subroutine. For more examples, see Lambda Expressions (Visual Basic).

Dim writeline1 = Sub(x) Console.WriteLine(x)
Dim writeline2 = Sub(x)
                     Console.WriteLine(x)
                 End Sub 

' Write "Hello".
writeline1("Hello")

' Write "World"
writeline2("World")
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.