[Public|Private||Protected|Friend|Protected Friend] Delegate Sub name [([arglist])] [Public|Private||Protected|Friend|Protected Friend] Delegate Function name [([arglist])] As type
The Delegate statement syntax has these parts:
Part | Description |
---|---|
Public | Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities. |
Private | Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities. |
Protected | Optional. Entities declared with the Protected modifier have protected access. Protected access can only be specified on members of types (both regular type members and nested types), although there are different access rules for the two. A protected type member contained in a type is accessible to entities contained in a derived type, provided the access takes place through the derived type. A protected type nested in a type is accessible to entities contained in a derived type, provided the access takes place through the base type (since nested types are not inherited). Protected access is not a superset of friend access. |
Friend | Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration. |
Protected Friend | Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility. |
name | Required. Name of the delegate type; follows standard variable naming conventions. |
arglist | Optional. List of variables representing arguments that are passed to the procedure when it is called. |
type | Optional. Data type of the return value. |
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname[( )] [As type]
Part | Description |
---|---|
ByVal | Optional. Indicates that the argument is passed by value. ByVal is the default in Visual Basic. |
ByRef | Optional. Indicates that the argument is passed by reference. |
Varname | Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions. |
( ) | Required for array variables. Indicates that varname is an array. |
type | Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Char, Short, Integer, Long, , Single, Double, Decimal, Date, String (variable length only), Object, a user-defined type, or an object type. |
The Delegate statement defines the parameter types and return type of a delegate class. Any procedure with matching parameters types and return type may be used to create an instance of this delegate class. The procedure can then later be invoked via the delegate instance. by calling the delegate’s Invoke method.
Each delegate class defines a constructor that is passed the specification of an object method. The only thing that can be legally specified as a argument to such a delegate constructor is an expression of the form:
AddressOf [<expression>.]<methodname>
The compile-time type of the expression must be the name of a class or an interface that contains a method of the specified name whose signature matches the signature of the delegate class. The methodname can be either a shared method or an instance method. The methodname is not optional even if you create a delegate for the default method of the class.
The method associated with a delegate instance is invoked by calling its built-in Invoke method, illustrated in the following example:
Delegate Sub MySubDelegate(x As Integer)
‘ Class 1
Sub Sub1(x As Integer)
. . .
End Sub
‘ Class 2
Test()
Dim c1 As Class1
Dim msd As MySubDelegate
c1 = New Class1
' Create an instance of the delegate.
Set msd = New MySubDelegate(AddressOf c1.Sub1)
' Call the method.
Call msd.Invoke(10)