How are return values from a delegate handled?

How are return values from a delegate handled?

  • Comments 4

Q: How are multiple return values from a delegate handled?

In C#, it's possible to write a delegate such as:

delegate double GetResult(params double p);

If there is more than one method on this delegate, there are multiple return values.

A: In this situation, the return value is the value that is returned from the last delegate.

If you want more control on this, you can call GetInvocationList() on the delegate, which will allow you to call each method individually, and process the double values appropriately.

[Author: Eric Gunnerson]

Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post
  • > If there is more than one method on this delegate

    I have to say this is a bit vague on me, do you mean that there are multiple methods "matching the signature" of that GetResult delegate?
  • Ahh.. I get what you mean now lol :) Need some coffee Doh
  • private void StartEvent_Click(object sender, System.EventArgs e)

    {

    if( StartEvent != null ){

    foreach(DemoEventHandler dlg in StartEvent.GetInvocationList()){

    try{

    if( (bool)dlg.DynamicInvoke(new object[]{this,e}) ){

    ......

    } else

    }

    ......

    {

    }

    catch( ApplicationException ex ){

    ...

    }

    }

    }

    }

  • public delegate bool DemoEventHandler(object sender, EventArgs e);

    public event DemoEventHandler StartEvent;



    private void StartEvent_Click(object sender, System.EventArgs e)

    {

    if( StartEvent != null ){

    foreach(DemoEventHandler dlg in StartEvent.GetInvocationList()){

    try{

    // Invoke Async call

    dlg.BeginInvoke(this,e,new AsyncCallback(CallBack),null );

    }

    catch( ApplicationException ex ){

    ???? ?? ????? ????? ??? ??????? ????? ?? ?????...

    }

    }

    }

    }





    private void CallBack( IAsyncResult ar )

    {

    AsyncResult aResult = (AsyncResult)ar;

    DemoEventHandler dlg = (DemoEventHandler)aResult.AsyncDelegate;



    // Complete the asynchronous call.

    if( dlg.EndInvoke( ar ) )

    ????? ?????? ?????? ??????......

    else

    ????? ?????? ?? ?????? ??????......

    }

Page 1 of 1 (4 items)