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:
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> |
|
|||||||
|