Delegate Definition

Syntax

[ | Private | Public ] _
Delegate Sub name [([param[, ...]])]
-or-
[ | Private | Public ] _
Delegate Function name[type] _
    [([param[, ...]])] [As type[()]]

Group

Declaration

Description

Define a new user delegate (subroutine or function pointer type). A delegate's Sub or Function is called using the delegate's Invoke method. If no access is specified then Public is assumed.

Parameters Description
name This is the name of the delegate being defined.
params A list of zero or more params that are used by the delegate's subroutine or function.

See also

AddressOf


Delegate Function OpType(ByVal v1 As Variant, ByVal v2 As Variant) As Variant
 
Sub Main
    Debug.Print DoOp(AddressOf Add,1,2) ' 3
    Debug.Print DoOp(AddressOf Subtract,1,2) '-1
End Sub
 
Function DoOp(ByVal op As OpType, _
              ByVal v1 As Variant, ByVal v2 As Variant) As Variant
    DoOp = op.Invoke(v1,v2)
End Function
 
Function Add(ByVal v1 As Variant, ByVal v2 As Variant) As Variant
    Add = v1+v2
End Function
 
Function Subtract(ByVal v1 As Variant, ByVal v2 As Variant) As Variant
    Subtract = v1-v2
End Function