Edit

Share via


'ByRef' parameter '<parametername>' cannot be used in a lambda expression

A lambda expression declared within a Sub or function cannot use any ByRef parameters of that Sub or function. For example, the following code will cause this error because the ByRef parameter n is used in the lambda expression.

VB
'' Not valid.
'Sub ExampleSub(ByRef n As Integer)  
  
'    Dim lambda = Function(p As Integer) p + n  
  
'End Sub  

Error ID: BC36639

To correct this error

  • Assign the ByRef parameter to a local variable, and use the local variable in the lambda expression, as shown in the following code:

    VB
    Sub ExampleSub(ByRef n As Integer)  
    
        Dim temp = n  
        Dim lambda = Function(p As Integer) p + temp  
    
    End Sub  
    

See also