Using delegates, one method can be indirectly passed as argument to another method. To proceed, first declare the necessary delegate. Here is a example of such a delegate: <script runat="server"> public delegate double Squared(double x); public class Circle { public double Radius; } </script> A delegate can be passed as argument to a method. Such an argument would be used as if it were a method itself. This means that, when accessed in the body of the method, 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 runat="server"> public delegate double Squared(double x); public class Circle { public double Radius; public double Area(Squared sqd) { return sqd(Radius) * Math.PI; } } </script> After declaring a delegate, remember to define a method that implements the needed behavior of that delegate. You can define the associated method in a class other than the one where the delegate would be used. Here is an example: <script runat="server"> public delegate double Squared(double x); public class Circle { public double Radius; public double Area(Squared sqd) { return sqd(Radius) * Math.PI; } } public class Exercise { public double ValueTimesValue(double Value) { return Value * Value; } } </script> You can also define the method in the class where the delegate would be needed. Once the method that implements the delegate is known, you can use the delegate as you see fit. To do that, you can declare a variable of the type of that delegate and assign it to the variable. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public delegate double Squared(double x); public class Circle { public double Radius; public double Area(Squared sqd) { return sqd(Radius) * Math.PI; } } public class Exercise { public double ValueTimesValue(double Value) { return Value * Value; } } </script> <title>Exercise</title> </head> <body> <% Exercise exo = new Exercise(); Squared sq = exo.ValueTimesValue; %> </body> </html> This declaration gives life to the delegate and can then be used as we have proceed with delegates so far. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public delegate double Squared(double x); public class Circle { public double Radius; public double Area(Squared sqd) { return sqd(Radius) * Math.PI; } } public class Exercise { public double ValueTimesValue(double Value) { return Value * Value; } } </script> <title>Exercise</title> </head> <body> <% Exercise exo = new Exercise(); Squared sq = exo.ValueTimesValue; Response.Write("Result = " + sq(204.72)); %> </body> </html> This would produce:
A delegate can be created to return a value that is of a class type. Of course you must know the class you want to use because the compiler would like to know the type of value that the delegate would return. You can use one of the many built-in classes of the .NET Framework or you can create your own class. Here is an example: <script runat="server"> public class Person { public string FirstName; public string LastName; } </script> When creating the delegate, specify the name of the class to its left as the returned type of value. After doing this, you can create a method that implements the delegate. The method must return the same type of value as the delegate. Here is an example: <script runat="server"> delegate Person Creator(); public class Person { public string FirstName; public string LastName; } public class People { public Person Create() { Person pers = new Person(); pers.FirstName = "Julius"; pers.LastName = "Krands"; return pers; } } </script> To use the delegate, declare a variable for it and assign the method to it. Here is an example: <%@ Page Language="C#" %> <html> <head> <script runat="server"> delegate Person Creator(); public class Person { public string FirstName; public string LastName; public string Show() { return "Affiche"; } } public class People { public Person Create() { Person pers = new Person(); pers.FirstName = "Julius"; pers.LastName = "Krands"; return pers; } } </script> <title>Exercise</title> </head> <body> <% People ppl = new People(); Creator crt = ppl.Create; %> </body> </html> You can then call use the variable as you see fit.
A delegate can be created to receive a class type as argument. When creating the delegate, in its parentheses, specify the class whose value it takes as argument. To use the delegate, you can first create a method that implements the delegate, then declare a variable for the delegate and assign the method to it. As done for primitive types:
|
|
|||||||||
|