Delegate Statement

This statement creates a Delegate class that provides a means for indirectly calling a function or subroutine procedure using an object variable.

[ Public | Private ] Delegate Functiondelegate_name([parameter_list]) As type
-or-
[ Public | Private ] Delegate Subdelegate_name([parameter_list])

Prerequisites

None

Parameters

delegate_name

The name of the Delegate class to be defined.

parameter_list

A template for the parameters that are passed to the procedure when it is called via a Delegate object. The number and type of the parameters in this list must match whatever procedure is subsequently associated with a Delegate object. The list may be empty if the procedure has no parameters. The names of the parameters in this list are not important. See the Function or Sub statement definitions for more details on parameters lists.

type

For Function procedures, this is the type of the value returned by the procedure associated with the Delegate object. This is not used if this Delegate is for a Sub procedure.

Remarks

Each Delegate statement defines a different Class that contains a template for indirectly executing a type of procedure. A program can create Delegate objects that contain pointers to Function or Sub procedures. These Delegate objects allow the associated Function or Sub procedures to be called indirectly.

Delegate statements are equivalent to Class declarations and may occur at the Module level or Class level.

The AddressOf operator is used when creating new Delegate objects. When a new Delegate object is created, the type of the procedure and the argument list of the procedure must be identical to the parameter_list and type specified in the corresponding Delegate statement. If a non-shared class method is specified, a reference to the object associated with that method is saved in the Delegate object and used when the Delegate is referenced.

Examples

Module GPL
Public Delegate Sub SubDel(ByVal arg As String, _
ByRef out As String)
Public Sub Test
Dim del(1) As SubDel
Dim ii As Integer
Dim ss As String
del(0) = New SubDel(AddressOf TypeA)
del(1) = New SubDel("TypeB")
For ii = 0 To 1
del(ii)("message", ss)
Console.WriteLine(ss)
Next ii
' Output is "A message", "B message"
End Sub

Public Sub TypeA(ByVal ins As String, ByRef outs As String)
outs = "A " & ins
End Sub

Public Sub TypeB(ByVal ins As String, ByRef outs As String)
outs = "B " & ins
End Sub
End Module

Module GPL2

Public Class D_Class
Public value As Double
Public Function Dcfun(ByVal arg As Integer) As String
Dim ss As String
ss = "Dcfun, value: " & CStr(value) & ", arg: " & CStr(arg)
Return ss
End Function
End Class

Public Delegate Function
FunDel(ByVal arg As Integer) As String

Public
Sub Test
Dim obj As New D_Class
Dim ss As String
Dim dc_del As FunDel
obj.value = 2
dc_del = New FunDel(AddressOf obj.Dcfun)

ss = dc_del(4)
Console.Writeline(ss) ' Output is "Dcfun, value: 2, arg: 4"
Console.Writeline(dc_del(4).Length) ' Output is "23"
End Sub
End Module

See Also

Statements |FunctionStatement| SubStatement