C# Frequently Asked Questions

The C# team posts answers to common questions

How are return values from a delegate handled?

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]

Published Thursday, May 27, 2004 3:50 PM by CSharpFAQ

Comments

 

Joku said:

> 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?
May 28, 2004 1:45 AM
 

Joku said:

Ahh.. I get what you mean now lol :) Need some coffee Doh
May 28, 2004 1:48 AM
 

Eyal said:

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 ){

...

}

}

}

}

June 26, 2004 10:06 PM
 

Eyal -Async said:

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

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

}

June 26, 2004 10:07 PM
Anonymous comments are disabled

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker