Used to declare a delegate. A delegate is a reference type that refers to a Shared method of a type or to an instance method of an object. Any procedure with matching parameter types and return type may be used to create an instance of this delegate class. The procedure can then later be invoked by means of the delegate instance.
[ <attrlist> ] [ accessmodifier ] _ [ Shadows ] Delegate [ Sub | Function ] name [( Of typeparamlist )] [([ parameterlist ])] [ As type ]
Optional. List of attributes that apply to this delegate. Multiple attributes are separated by commas. You must enclose thein angle brackets ("<" and ">").
Optional. Specifies what code can access the delegate. Can be one of the following:
Public. Any code that can access the element that declares the delegate can access it.
Protected. Only code within the delegate's class or a derived class can access it.
Friend. Only code within the same assembly can access the delegate.
Private. Only code within the element that declares the delegate can access it.
You can specify Protected Friend to allow access from code within the delegate's class, a derived class, or the same assembly.
Optional. Indicates that this delegate redeclares and hides an identically named programming element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind.
A shadowed element is unavailable from within the derived class that shadows it, except from where the shadowing element is inaccessible. For example, if a Private element shadows a base class element, code that does not have permission to access the Private element accesses the base class element instead.
Optional, but either Sub or Function must appear. Declares this procedure as a delegate Sub procedure that does not return a value.
Optional, but either Sub or Function must appear. Declares this procedure as a delegate Function procedure that returns a value.
Required. Name of the delegate type; follows standard variable naming conventions.
Optional. List of type parameters for this delegate. Multiple type parameters are separated by commas. You must enclose thein parentheses and introduce it with the Of keyword.
Optional. List of parameters that are passed to the procedure when it is called. You must enclose thein parentheses.
Required if you specify a Function procedure. Data type of the return value.
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 by means of the delegate instance, by calling the delegate's Invoke method.
Delegates can be declared at the namespace, module, class, or structure level, but not within a procedure.
Each delegate class defines a constructor that is passed the specification of an object method. An argument to a delegate constructor must be an expression of the form:
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 following example uses the Delegate statement to declare a delegate for operating on two numbers and returning a number. The DelegateTest method takes an instance of a delegate of this type and uses it to operate on pairs of numbers.
Delegate Function MathOperator( _ ByVal x As Double, _ ByVal y As Double _ ) As Double Function AddNumbers( _ ByVal x As Double, _ ByVal y As Double _ ) As Double Return x + y End Function Function SubtractNumbers( _ ByVal x As Double, _ ByVal y As Double _ ) As Double Return x - y End Function Sub DelegateTest( _ ByVal x As Double, _ ByVal op As MathOperator, _ ByVal y As Double _ ) Dim ret As Double ret = op.Invoke(x, y) ' Call the method. MsgBox(ret) End Sub Protected Sub Test() DelegateTest(5, AddressOf AddNumbers, 3) DelegateTest(9, AddressOf SubtractNumbers, 3) End Sub