Sunday 12 February 2012

Fundamentals of Delegates with ASP.net

A delegate is a type of user-defined variable that is declared globally, like a class, providing a template for a method. Unlike a method, a delegate is not actually defined. Its role is to give a view of a useful method. To represent a method, a delegate provides the necessary information that would be used on a method, including a return type, no argument or one or more arguments.
 
Creating a Delegate



The basic formula used to create a delegate is:
[attributes] [modifiers] delegate ReturnType Name ([formal-parameters]);

The attributes can be a normal attribute.

The modifier(s) can be one or an appropriate combination of the following keywords: new, public, private, protected, or internal.

The delegate keyword is required.

The ReturnType can be any of the data types we have used so far. It can also be a type void or the name of a class.

The Name must be a valid variable name.
Because a delegate is some type of a template for a method, you must use parentheses. If this method will not take any argument, leave the parentheses empty.

Here is an example:
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
public delegate void Simple();

</script>

<title>Exercise</title>
</head>
<body>

</body>
</html>

A delegate only provides a template for a method, not an actual method. In order to use it, you must define a method that would carry an assignment. That method must have the same return type and the same (number of) argument(s), if any. Here is an example:
 
<script runat="server">
public delegate void Simple();

public class Exercise
{
    public void Welcome()
    {
    }
}
</script>

With such a method implemented, you can associate it to the name of the delegate. To do that, where you want to use the method, declare a variable of the type of the delegate and assign the method to the delegate variable. Because you are assigning the method to a delegate, one of the rules of delegates is that you must not apply the parentheses to the method. Here is an example
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public delegate void Simple();

public class Exercise
{
    public void Welcome()
    {        
    }
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();

    Simple msg = exo.Welcome;
%>
</body>
</html>

Accessing a Delegate

 
Introduction


Once you have assigned a method to a delegate variable, you can use the delegate variable as if it were a defined method. That is, you can call it as you would call a normal method. Here is an example:
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public delegate void Simple();

public class Exercise
{
    public void Welcome()
    {        
    }
}
</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();

    Simple msg = exo.Welcome;

    msg();
%>
</body>
</html>

An Anonymous Delegate


In the above examples, we had to create a method that would be associated with a delegate. Alternatively, you can create a delegate. Then, when you need to use it, create a type of local implementation of a method and use it. Such a method is referred to as anonymous.
Before implementing an anonymous method, first declare the delegate you will use:
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
public delegate void Simple();

</script>

<title>Exercise</title>
</head>
<body>

<%
    
%>
</body>
</html>

To create an anonymous method, declare a variable for the delegate and assign it the delegate keyword as if it were a method. That is, followed by parentheses and curly brackets that would represent the body of the method. In the body of the anonymous method, do whatever you want. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>

 
<script runat="server">

public delegate void Simple();

</script>

<title>Exercise</title>
</head>
<body>

<%
    Simple msg = delegate()
    {
        Response.Write
("Welcome to the Wonderful World of C# Programming!");
    };
%>
</body>
</html>

Once you have done this, you can then call the delegate variable as if it were a normal method. Here is an example:
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public delegate void Simple();

</script>

<title>Exercise</title>
</head>
<body>

<%
    Simple msg = delegate()
    {
        Response.Write
("ASP.NET Web Development With C#!");
    };

    msg();
%>
</body>
</html>

This would produce:
Delegate

A Delegate that Returns a Value


You can create a delegate that returns a value. When creating the delegate, specify the data type to the left side of the name of the delegate. When defining a method that would be associated with the delegate, remember that the method must return the same type of value. In the body of the method, use it as you see fit. Before exiting the method, make sure you appropriately return a value.
To use the method, follow the same approach as above. This time, when calling the delegate variable, you should use the parentheses. Here is an example:
 
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">
delegate double Doubler();

public class Exercise
{
    public double MultiplyBy2()
    {
        return 1425 * 2;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();
    Doubler By2 = exo.MultiplyBy2;

    Response.Write("Number = " + By2());
%>
</body>
</html>

This would produce:
Delegate

Delegates Compositions


One of the characteristics of delegates is that one delegate can be added to another using the + operation. This is referred to as composition. This is done by adding one delegate variable to another as in a = b + c.
Delegates and Arguments

 
Introduction

If you want to use a method that takes arguments and associate it 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):
<script runat="server">

delegate double Doubler(double a);

</script>
When defining the associated method, besides returning the same type of value if not void, make sure that the method takes the same number of arguments. Here is an example:
<script runat="server">

delegate double Doubler();

public class Exercise
{
    public double MultiplyBy2(double x)
    {
        return x * 2;
    }
}

</script>
Using an Argumentative Delegate

To associate the method to the delegate, you can declare a variable for the delegate and assign the name of the method to it. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

delegate double Doubler(double a);

public class Exercise
{
    public double MultiplyBy2(double x)
    {
        return x * 2;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();
    Doubler By2 = exo.MultiplyBy2;
%>
</body>
</html>
Notice that only the name of the method is assigned to the delegate. To actually use the delegate, when calling it, add the parentheses to it and in the parentheses, provide a value for the argument(s). Here is an example:
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

delegate double Doubler(double a);

public class Exercise
{
    public double MultiplyBy2(double x)
    {
        return x * 2;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Exercise exo = new Exercise();
    Doubler By2 = exo.MultiplyBy2;

    Response.Write("Number = " + By2(418.62));
%>
</body>
</html>
This would produce:
Delegate
An Anonymous Delegate that Takes an Argument

You can create an anonymous method for a delegate that takes one or more arguments. You can do this using the delegate keyword. In its parentheses, pass an argument that is the same type as the argument of the delegate. Then, in the body of the method, do what you judge necessary. When calling the variable of the delegate, use the same rules we have applied so far. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

delegate double Doubler(double x);

</script>

<title>Exercise</title>
</head>
<body>

<%
    Doubler dbl = delegate(double alpha)
    {
        return alpha * 2;
    };

    Response.Write("Result = " + dbl(248.26));
%>
</body>
</html>
A Delegate With Many Arguments

A delegate can take more than one argument. To start, when declaring it, pass the desired number of arguments. If you will use a method to associate to the delegate, then create the method also. Here is an example:
<script runat="server">delegate double Addition(double x, double y);

public class Algebra
{
    public double Plus(double a, double b)
    {
        return a + b;
    }
}
</script>
To use the delegate, follow the techniques we have applied so far and call the delegate as a method. Here is an example:
<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

delegate double Addition(double x, double y);

public class Algebra
{
    public double Plus(double a, double b)
    {
        return a + b;
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Algebra alg = new Algebra();
    Addition Operation = alg.Plus;

    Response.Write("Result = " + Operation(52.04, 9.368));
%>
</body>
</html>
This would produce:
Delegate
 

 


 
 
A Delegate Passed as Argument

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:
Delegate
Delegates and Classes

 
A Delegate that Returns an Object

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 that Takes an Object as Argument

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:
  • You can create a delegate that takes a class as argument and returns a class type
  • You can create a delegate that takes more than one argument. One of the arguments could be a class type and the other(s) a class or a primitive type

 
 

   

No comments :