A delegate is a special type of user-defined variable that
is declared globally, like a class. A delegate provides a prototype for a procedure, a function, or a method. Like
an interface, a delegate is not defined. Its role is to provide a syntax for a procedure or a function.

Delegates for Sub-Procedures

As you should know already, the Visual Basic language has two categories of procedures: sub-procedures and functions. Delegates also distinguish them. The basic formula to create a delegate for a sub-procedure is:

[modifier]Delegate Subname()

The modifier can be Public, Private, or Friend. The Delegate keyword is required. The name must be a valid name for a procedure or a function. Because a delegate represents a procedure, it must use parentheses. If the procedure will not take any argument, leave the parentheses empty.

Here is an example of creating a delegate:

Delegate Sub Definition()

Remember that a delegate only provides a template for a procedure, not an actual function. Before using it, you must
define a procedure that would carry an assignment. Here is an example:

<script runat="server">
Public Sub Describe()
Response.Write("<p>The metric system is a set of rules that provide " &
"common standards for different countries, organizations, " &
"and cultures to share numeric values. The values address " &
"length, mass, and time.</p>")
End Sub
</script>

After implementing the procedure, you can associate it to
the delegate. To do that, where you want to use the procedure, declare a variable of the type of the delegate. Here is an example:

<%
Dim metric As Definition
%>

To initialize the variable, use the New operator followed by the name of the delegate and parentheses. In
the parentheses, you must access the procedure by its address. To let you do this, the Visual Basic language provides the AddressOf operator. It is followed by the name of the procedure. Once a delegate has been made aware of the procedure that implements it, you can use the variable as if calling a procedure. That is, you can write the name of the variable followed by parentheses. Here is an example:

As it is possible in Visual Basic, you don't have to separately declare the variable and initialize it. You can use the As New clause to declare and initialize the variable. Here is an example:

<%
Dim metric As New Definition(AddressOf Describe)
metric()
%>

A Delegate With a Parameter for a Sub-Procedure

When necessary, a delegate can take one or more parameters. The basic formula to create a delegate that takes a parameter is:

[modifier]Delegate Subname(parameter(s))

Based on this, in the parentheses of the delegates, specify the parameter by its name and its type. When creating the related procedure, you must also specify the same type of parameter. When calling the associate variable, make sure you pass an appropriate value for the argument. Here is an example:

You can pass as many parameters as you need to a delegate. When creating the associated procedure, make sure you specify the same number, type, and order of parameters. When calling the procedure on the variable, make sure you pass the same number, type, and order of arguments. Here is an example:

Instead of making you create delegates from scratch, and to
provide universal support for all .NET languages, the .NET Framework provides various pre-built
or built-in delegates that you can use directly in your code. One of the
built-in delegates is named Action. It is provided in various
versions. The Action delegate is defined in the System namespace of the mscorlib.dll
library.

Creating a Simple Action

The primary type of an Action delegate is for
sub-procedures that take no parameter and return nothing. Its syntax is:

Public Delegate Sub Action

Notice that the syntax exactly resembles the one we saw
in our introduction to delegates. This time, the name of the delegate is replaced with Action.
Also notice that the Delegate Sub expression is used, exactly as it was in
our introduction to delegates.

To create an action for the delegate, declare a variable.
Since you would not have created a delegate, use Action to
initialize the variable and add its parentheses. Here is an example:

<%
Dim metric As Action()
%>

The rest is done as we saw already. This means that you
should first create a procedure. In the parentheses of Action,
type AddressOf followed by the name of the procedure. Based on this
description, this:

Like regular delegates, the Action delegate can take a parameter. To support this, a version of this delegate uses the following syntax:

Public Delegate Sub Action(Of In T)(obj As T)

Notice that this Action delegate is generic. This means that it expects any type of value. As a result, when declaring the variable, you must indicate the type of value that will be used. This is done by adding a first set of parentheses to Action. In the parentheses, type Of followed by the type. Here is an example:

<%
Dim number As New Action(Of Integer)(AddressOf ...)
%>

In the second set of parentheses, type AddressOf followed by the procedure that implements the delegate. As a result, this:

To support parameters passed to a delegate, the Action
delegate provides many versions, each for a certain number of parameters. The version that takes two arguments uses the following syntax:

Public Delegate Sub Action(Of In T1, In T2)(arg1 As T1, arg2 As T2)

This syntax is for a sub-procedure that takes two arguments. As you can see,
the parameters are generic types. When calling Action, once again, add two sets of parentheses. In the first set, type Of followed by the types of values separated by commas. When calling the variable as a procedure, make sure you pass the right types of arguments. As a result, this:

Of course, the delegate (and its procedure) can take different types of arguments. To apply this, in the first set of parentheses of Action, type Of and the order of types. When calling the variable as a procedure, in its parentheses, make sure you provide the arguments in the appropriate order. Here is an example:

Another category of delegates focuses on functions. These are delegates that can produce a value. Remember that a delegate only provides a layout for a function but it not the function itself. As seen for procedures, you must have a function to which the delegate will point. Here is an example of a function:

<script runat="server">
Function Describe() As String
Return "<p>Chemistry is the study of matter. Matter is the building block of anything in the environment. " &
"Chemistry by examines the smallest object that starts a matter, which is the atom. Chemistry " &
"explores the interactions between two atoms and among atoms. The study seeks to understand " &
"how an atom of a kind, called a substance, combines with another atom of the same substance, " &
"or how an atom of one substance combines with one or other atoms of other substances to produce " &
"a new substance. A combination of atoms is also called a bond. The resulting object is called a " &
"compound. Of course, there are rules that govern everything.</p>"
End Function
</script>

The basic formula to create a delegate for a parameter-less function is:

[modifier]Delegate Functionname() Asreturn-type

The approach is the same as seen for procedures. When creating the delegate, make it return the same type of value as the function. When declaring the variable, use the New operator to indicate the name of the delegate. In the parentheses, add the AddressOf operator followed by the name of the function. You can then use the variable as a function and get its returned value. Here is an example:

<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head runat="server">
<script runat="server">
Delegate Function Introduction() As String
Function Describe() As String
Return "<p>Chemistry is the study of matter. Matter is the building block of anything in the environment. " &
"Chemistry by examines the smallest object that starts a matter, which is the atom. Chemistry " &
"explores the interactions between two atoms and among atoms. The study seeks to understand " &
"how an atom of a kind, called a substance, combines with another atom of the same substance, " &
"or how an atom of one substance combines with one or other atoms of other substances to produce " &
"a new substance. A combination of atoms is also called a bond. The resulting object is called a " &
"compound. Of course, there are rules that govern everything.</p>"
End Function
</script>
<title>Chemistry</title>
</head>
<body>
<%
Dim lesson As New Introduction(AddressOf Describe)
Response.Write(lesson())
%>
</body>
</html>

This would produce:

A Function Delegate With a Parameter

A function delegate can take a parameter, and the parameter can be any type you want. Make sure both the delegate and its associated function take the same type of parameter. Here is an example:

Instead of one parameter, a function delegate can take as many parameters as you want. For both the delegate and the associated function, make sure you follow the rules of passing arguments to a procedure or a function. Here are examples of delegates that each take two arguments of the same type:

Instead of maing you create delegate functions from scratch, to support functions and methods that return a value, the .NET Framework provides a delegate named Func. As seen for the Action type, the Func delegate is delivered in various versions that take different parameters.

A Simple Function

The most fundamental version of a function delegate uses the following syntax:

Public Delegate Function Func(Of Out TResult) As TResult

This time, instead of the Action word, Func is used. This version is for a function or method that takes no parameter but returns a value. The return value is a generic type. This means that you must specify the type of value that the function or method must return. To use this version of the function, don't declare a delegate. In the section where you need it, declare a variable an initialize it using Func preceded by the New operator. As seen with the action delegate, Func is followed by two sets of parentheses. The first set contains Of followed by the return type of the function. In the second set, type AddressOf followed by the name of the function you want to use. You can then use the variable as when calling a function that returns a value. For example, you can assign it to a variable. Here is an example:

<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head runat="server">
<script runat="server">
Function Describe() As String
Return "<p>Chemistry is the study of matter. Matter is the building block of anything in the environment. " &
"Chemistry by examines the smallest object that starts a matter, which is the atom. Chemistry " &
"explores the interactions between two atoms and among atoms. The study seeks to understand " &
"how an atom of a kind, called a substance, combines with another atom of the same substance, " &
"or how an atom of one substance combines with one or other atoms of other substances to produce " &
"a new substance. A combination of atoms is also called a bond. The resulting object is called a " &
"compound. Of course, there are rules that govern everything.</p>"
End Function
</script>
<title>Chemistry</title>
</head>
<body>
<%
Dim lešon As String
Dim lesson As New Func(Of String)(AddressOf Describe)
lešon = lesson()
Response.Write(lešon)
%>
</body>
</html>

If you don't need to use the return value many times, you don't have to store it in a variable. You can call it directly where the return value is needed. Here is an example:

In the same way, you don't have to formally declare a variable for Func. You can use and call it directly where needed. Here is an example:

<%
Response.Write((New Func(Of String)(AddressOf Describe))())
%>

A Function Delegate With a Parameter

Like a procedure, a function delegate can take a parameter, of any type. To support this, the overloaded Func delegate has a version whose syntax is:

Public Delegate Function Func(Of In T, Out TResult)(arg As T) As TResult

Of course, you must have a function that takes one parameter. When declaring the variable, after the Func keyword and its parentheses, in the first set, type Of followed by the type of the argument, a comma, and the return type of the function. When calling the variable as a function, pass the argument as the first type of Func. Here is an example:

Remember that the syntax of Func indicates that the first parameter is the actual argument while the second it the return type. This means that you must make sure you specify the types in the right order of Func and the pass the right type of argument when accessing the variable as a function. Here is an example: