Creating and Invoking Delegates


Once you declare a delegate, you will want to allocate an instance of the delegate class and assign a function to it. Then you can invoke the particular function using the delegate variable.

To create a delegate:

  1. Declare a variable using the delegate name as its type.

  2. Type an equal sign = .

  3. Type new followed by the name of the delegate type.

  4. Type an open parenthesis ( .

  5. Type the name of the function that the delegate will store. If the function is an instance function type var.functioname , where var is a variable storing an instance of the class and functionname is the name of the function that you want to invoke through the delegate. If the function is a static function, type classname.functionname where classname is the name of the class and functionname is the name of the static function you want to invoke.

  6. Type a close parenthesis ) .

  7. Type a semicolon ; ( Figure 10.6 ).

    Figure 10.6 Once you declare a delegate, you can create instances of it passing a function as the parameter of the constructor. The function can be an instance function, like Task1, or a static function like Task2.
     class Tasks {    public bool Task1(string desc)    {       return true;    }    public static bool Task2(string desc)    {       return true;    } }  delegate bool TaskDel(string desc);  public class WebForm1 : System.Web.UI.Page {    private void Page_Load(object sender,                 System.EventArgs e)    {  Tasks tsks = new Tasks();   TaskDel t1 = new TaskDel(   tsks.Task1);   TaskDel t2 = new TaskDel(   Tasks.Task2);  } } 

To invoke a delegate:

  1. Type the name of the variable that points to a delegate object.

  2. Type an open parenthesis ( .

  3. Enter the parameters to pass to the function assigned to the delegate object.

  4. Type a close parenthesis ) ( Figure 10.7 ).

    Figure 10.7 Once you assign a function to the delegate variable, you can call it by using the variable followed by parenthesis and the parameters of the function that the delegate is storing.
     class Tasks {    public bool Task1(string desc)    {       return true;    }    public static bool Task2(string desc)    {       return true;    } } delegate bool TaskDel(string desc); public class WebForm1 : System.Web.UI.Page {    private void Page_Load(object sender,                 System.EventArgs e)    {       Tasks tsks = new Tasks();       TaskDel t1 = new TaskDel(                       tsks.Task1);       TaskDel t2 = new TaskDel(                       Tasks.Task2);  bool result;   result = t1("Calling Task 1...");   result = t2("Calling Task 2...");  } } 

graphics/tick.gif Tip

  • You may recall that a delegate declaration is really a class declaration. The Invoke method is a method that is part of the class. It has the same parameters and output parameters as the delegate declaration. When you use the variable that stores the delegate followed by parenthesis, the compiler turns that code into a call to the Invoke method, passing the parameters after the parenthesis.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net