473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Delegates with out parameter

What is meant by this statement?

"Although the delegate can use an out parameter, it is not recommended to
use it with multicast event delegates because there is no way to know which
delegate will be called."

regards
AB
May 24 '06 #1
5 6524
Amit,

Basically, the idea is that if you have an out parameter on a delegate,
you can't be sure of what that out parameter is (or ref parameters, for that
matter).

Because a delegate can have multiple functions that are called when it
is invoked, if every routine set the out parameter to something different,
then you would lose the value set by all of the other routines.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Amit Bansal (MCT, MCSD.NET)" <te*******@peoplewareindia.com> wrote in
message news:OH*************@TK2MSFTNGP05.phx.gbl...
What is meant by this statement?

"Although the delegate can use an out parameter, it is not recommended to
use it with multicast event delegates because there is no way to know
which
delegate will be called."

regards
AB

May 24 '06 #2
Of course, you can always get the invocation list and call them individually
(and thus having each returned value in sequence), but I'm still not sure
that I would recommend it as an approach ;-p

Marc
May 24 '06 #3
Basically, when you write:

this.MyEvent += new MyEventDelegate(Function1);
this.MyEvent += new MyEventDelegate(Function2);

int a;
this.MyEvent(out a);

It's the equivalent of writing:

int a;
this.Function1(out a);
this.Function2(out a);

So, that the value written into a in Function1 is overwritten by the
value written by Function2. To make matters worse, you have no way of
knowing which function will be called first.

May 24 '06 #4
HI Nicholas,

Thanks for your mail. It has to do more with multicast delegates only. I
just spent some time on it:-

---------------
public delegate void Del(out string message);

public partial class OutDelegate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Del handler = DelegateMethod;
string name;
handler(out name);
Response.Write(name);

}

private void DelegateMethod(out string message)
{
message = "Amit";
}

}

-----------

Now in the above example, if 'handler' is calling a number methods of the
same signature, the parameter 'name' will have different value everytime. So
for every method it calls, there has to be a sepearte parameter to store the
value returned by the method call. So, the documentation says to avoid using
out parameter in case of multicast delegate. I hope I have understood it
right :)

Thanks
AB
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:eP**************@TK2MSFTNGP05.phx.gbl...
Amit,

Basically, the idea is that if you have an out parameter on a delegate, you can't be sure of what that out parameter is (or ref parameters, for that matter).

Because a delegate can have multiple functions that are called when it
is invoked, if every routine set the out parameter to something different,
then you would lose the value set by all of the other routines.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Amit Bansal (MCT, MCSD.NET)" <te*******@peoplewareindia.com> wrote in
message news:OH*************@TK2MSFTNGP05.phx.gbl...
What is meant by this statement?

"Although the delegate can use an out parameter, it is not recommended to use it with multicast event delegates because there is no way to know
which
delegate will be called."

regards
AB


May 24 '06 #5
_DD
On 24 May 2006 08:47:23 -0700, "ja**********@gmail.com"
<ja**********@gmail.com> wrote:
int a;
this.Function1(out a);
this.Function2(out a);

So, that the value written into a in Function1 is overwritten by the
value written by Function2. To make matters worse, you have no way of
knowing which function will be called first.


It seems much the same case as with a delegate that returns a value
(normal return, not an 'out'). But I thought that it was specified
that the last function named would determine the return value. Would
this not also be the case with 'out' params?
May 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
6
by: Wayne Weeks | last post by:
I am trying to use delegates for the first time and I am probably missing something. I have a legacy DLL and I have used DllImport to get to the functions that it contains...the normal function...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
10
by: Roger Frost | last post by:
Since we are currently on the subject of multi-threading in a different thread, I have a question about delegates. When I use delegates, I just create one for each different parameter set that I...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.