Home

Delegates

 

Delegates Fundamentals

 

Introduction

As done when you declare a variable, when you create and call a procedure, it uses a section of memory of the computer. This section is its address. You can access that area of memory to get to the procedure.

A function pointer is a special type of variable that is used to access a procedure using its address. To support function pointers, the .NET Framework provides the concept of delegates. A delegate provides a syntax for a procedure.

Creating a Delegate

You can first create a delegate before using one. In the same way, you can use one of the built-in delegates of the .NET Framework. The basic formula to create a delegate is:

[modifier] Delegate Sub/Function Name (parameter(s)) As ReturnType

The modifier factor can be Public, Private, or Friend. This is followed by either the Delegate Sub or the Delegate Function expression. A delegate must have a name. The name follows the rules of names in Visual Basic. The name of the delegate is followed by parentheses. You can leave the parentheses empty if there is no argument.

If the delegate will be associated with a sub-procedure, there is no return type. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

If the delegate is a function-type, you must specify a ReturnType either as a primitive type or a class. If you create a procedure that would be associated with a delegate, you can also define it as Shared.

Before using a delegate, you must create a procedure it will be associated with. The procedure must use the same syntax as the delegate. If the delegate will associate to a sub-procedure, the delegate also must be a sub-procedure. If there is no argument, neither the delegate nor the procedure would take an argument.

Here is an example:

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()

Private Sub ShowMessage()
    Response.Write("Main message")
End Sub

</script>

Getting the Address of a Procedure

To associate a procedure to a delegate, you must assign the address of the procedure to a variable declared from the delegate. This means that you must first declare a variable for the delegate.

To assist you with getting the address of a procedure, the Visual Basic language provides the AddressOf operator. To use it, create an expression that starts with the AddressOf operator followed by the name of the procedure. Assign that expression to the delegate's variable. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()

Private Sub ShowMessage()
    Response.Write("Main message")
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Mess As Messenger

    Mess = AddressOf ShowMessage
%>

</body>
</html>

After assigning the address of the procedure to the delegate's variable, you can call the delegate as if it were a procedure. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Messenger()

Private Sub ShowMessage()
    Response.Write("Welcome to our web site")
End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Mess As Messenger

    Mess = AddressOf ShowMessage
    Mess()
%>

</body>
</html>

This would produce:

Delegate

In our example, we used a sub-procedure. In the same way, if necessary, you can create a delegate as a function. In this case, you would define a function to associate to that delegate. Then, you can use the same techniques we followed to associate them.

Delegates and Classes

When it comes to delegates, not only can you use a normal procedure, you can use the method of a class to associate to a delegate. You can start by creating the class and the necessary method. Here is an example:

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Sub Simple()

Private Class Example

End Class

</script>

Of course, before using the delegate, you must declare it. This is done exactly as previously. The delegate must be of the same type as the method (sub-procedure or function) of the class.

Before associating the delegate to the intended method, declare a variable for it. Also declare a variable for the class. To associate the method to the delegate, get the address of the method, through its object, and assign it to the variable of the delegate. Once you have done this, you can call the delegate's variable as if it were a procedure.

A delegate is actually a class. The class is named Delegate. Based on this, whenever you create a delegate, the name you give it is considered an object. That is, the name you give to the delegate becomes an object of type Delegate. Based on the characteristics of classes, that delegate object automatically becomes equipped with methods.

 

 

 

 

Delegates and Arguments

 

Introduction

If you want to associate a procedure that takes arguments to a delegate, when declaring the delegate, provide the necessary argument(s) in its parentheses. Here is an example of a delegate that takes two arguments (and returns a value):

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Function Multiplication(ByVal value As Double) As Double

</script>

<title>Exercise</title>

</head>
<body>

</body>
</html>

When defining the associated procedure, besides returning the same type of value, make sure that the procedure takes the same number of arguments. Here is an example:

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Function Multiplication(ByVal value As Double) As Double

Public Function Area(ByVal Side As Double) As Double
    Return 6 * Side * Side
End Function

</script>

To associate the procedure, declare a variable of the type of delegate and access the name of the procedure using the AddressOf operator. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Function Multiplication(ByVal value As Double) As Double

Public Function Area(ByVal Side As Double) As Double
    Return 6 * Side * Side
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim AreaDefinition As Multiplication = New Multiplication(AddressOf Area)
%>

</body>
</html>

To assist you with accessing a delegate, the Delegate class is equipped with a method named Invoke. Therefore, to associate a method to a delegate, call this method on the variable of the delegate. The syntax of the Invoke() method is:

Public Function Invoke(method As Delegate) As Object

Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Function Multiplication(ByVal value As Double) As Double

Public Function Area(ByVal Side As Double) As Double
    Return 6 * Side * Side
End Function

Public Function Volume(ByVal Side As Double) As Double
    Return Side * Side * Side
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Side As Double = 46.95
    Dim AreaDefinition As Multiplication
    Dim VolDefinition As Multiplication

    AreaDefinition = New Multiplication(AddressOf Area)
    VolDefinition = New Multiplication(AddressOf Volume)

    Response.Write("Cube Calculation" & "<br />" & _
                   "Side: " & CStr(Side) & "<br />" & _
                   "Area: " & CStr(AreaDefinition.Invoke(Side)) & "<br />" & _
                   "Volume: " & CStr(VolDefinition.Invoke(Side)))
%>

</body>
</html>

This would produce:

Delegate

A Delegate Passed as Argument

Using delegates, a procedure can be indirectly passed as argument to another procedure. To proceed, first declare the necessary delegate. Here is a example of such a delegate:

<script language="vbscript" type="text/vbsscript" runat="server">

Delegate Function Squared(ByVal value As Double) As Double

</script>

A delegate can be passed as argument to a procedure. Such an argument would be used as if it were a procedure itself. This means that, when accessed in the body of the procedure, the name of the delegate must be accompanied by parentheses and if the delegate takes an argument or arguments, the argument(s) must be provided in the parentheses of the called delegate. Here is an example:

<script language="vbscript" type="text/vbsscript" runat="server">
Private Radius As Double

Delegate Function Squared(ByVal value As Double) As Double

Public Function Area(ByVal sqd As Squared) As Double
    Return sqd(Radius) * Math.PI
End Function
</script>

After declaring a delegate, remember to define a procedure that implements the needed behavior of that delegate. Once the procedure is ready, to associate it to a delegate, declare a variable of the type of the delegate using the New operator. In the parentheses of the constructor, access the name of the associated procedure using the AddressOf operator. To access the delegate, call the Invoke() method. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Private Radius As Double

Delegate Function Squared(ByVal value As Double) As Double

Public Function Area(ByVal sqd As Squared) As Double
    Return sqd(Radius) * Math.PI
End Function

Public Function ValueTimesValue(ByVal value As Double) As Double
    Return value * value
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Side As Double = 46.95
    Dim AreaDefinition As Squared

    AreaDefinition = New Squared(AddressOf ValueTimesValue)

    Response.Write("Circle Calculation" & "<br />" & _
                   "Side: " & CStr(Side) & "<br />" & _
                   "Area: " & CStr(AreaDefinition.Invoke(Side)))
%>

</body>
</html>
 
 
   
 

Home Copyright 2009 FunctionX, Inc.