473,387 Members | 1,569 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,387 software developers and data experts.

MultiCast Delegates

GVN
Hi All,

I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:

I have a delegate myDelegate with object dlgt, and two methods
myMethod1(), and myMethod2().

Using multicast delegates definition I can write as follows to add the
two methods to my multicast delegate:

dlgt += new myDelegate(myMethod1);
dlgt += new myDelegate(myMethod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMethod1);

Inside my myMethod1() I will call myMethod2() method.

What is the difference among the above two cases?

Thanks in advance....

Nov 24 '06 #1
5 3856
>What is the difference among the above two cases?

The obvious difference is that in the latter case, you have to know
that myMethod1 should call myMethod2. Delegates are often used for
events where you don't know who else might be interested in being
notified.

A more subtle difference is that in the former case, there's no
guarantee that myMethod1 will get called before myMethod2.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 24 '06 #2
Hi
As I see this you by calling method2 from method1,

1. generally delegates can be assigned methods from out side of the
class. as one can not expect how many of the methods would be assigned
to delegate (from client code of delegate) you can never keep on
calling one method from another and another and another.

2. I agree with Mattias point... myclass (that has delegate) will never
know who are interested in getting notified....

The obvious difference is that in the latter case, you have to know
that myMethod1 should call myMethod2. Delegates are often used for
events where you don't know who else might be interested in being
notified.

one can avoid these two points but that will introduce tight-coupling
between the classes of the application.

Thanks
-Srinivas.

GVN wrote:
Hi All,

I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:

I have a delegate myDelegate with object dlgt, and two methods
myMethod1(), and myMethod2().

Using multicast delegates definition I can write as follows to add the
two methods to my multicast delegate:

dlgt += new myDelegate(myMethod1);
dlgt += new myDelegate(myMethod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMethod1);

Inside my myMethod1() I will call myMethod2() method.

What is the difference among the above two cases?

Thanks in advance....
Nov 24 '06 #3
GVN
I thank both Duggi and Srinivas.

I found in both of your postings that:
"Delegates are often used for events where you don't know who else
might be interested in being notified. "

Can you explain me in detail about the above statement? I am still in a
dilemma about the above statement.
My dilemma is that:
The user will declare the skeleton of the delegate. He himself adds
some method(s) to the delegate. He will assign the delegate invocation
to some of the event. Then, why don't we know who else might be
interested in being notified?

Duggi wrote:
Hi
As I see this you by calling method2 from method1,

1. generally delegates can be assigned methods from out side of the
class. as one can not expect how many of the methods would be assigned
to delegate (from client code of delegate) you can never keep on
calling one method from another and another and another.

2. I agree with Mattias point... myclass (that has delegate) will never
know who are interested in getting notified....

The obvious difference is that in the latter case, you have to know
that myMethod1 should call myMethod2. Delegates are often used for
events where you don't know who else might be interested in being
notified.

one can avoid these two points but that will introduce tight-coupling
between the classes of the application.

Thanks
-Srinivas.

GVN wrote:
Hi All,

I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:

I have a delegate myDelegate with object dlgt, and two methods
myMethod1(), and myMethod2().

Using multicast delegates definition I can write as follows to add the
two methods to my multicast delegate:

dlgt += new myDelegate(myMethod1);
dlgt += new myDelegate(myMethod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMethod1);

Inside my myMethod1() I will call myMethod2() method.

What is the difference among the above two cases?

Thanks in advance....
Nov 25 '06 #4
Think of the Button class. You typically need to tell the call what it
needs to do whenever it is clicked.

Button foo = new Button();
foo.Click += ExecuteMe(Object sender, EventArgs e);

The author of the button class has no idea what you are going to call - but
yet you can call whatever you like without changing any of the original
Button code.

So then the next question is why would you do

Button foo = new Button();
foo.Click += ExecuteMe(Object sender, EventArgs e);
foo.Click += ExecuteMeAlso(Object sender, EventArgs e);

instead of calling ExecuteMeAlso from inside ExecuteMe?

The answer is that it is a design choice. If the only thing that ExecuteMe
and ExecuteMeAlso have in common is that they both need to execute when the
button is clicked, I would keep them seperate. On the other hand, if
ExecuteMeAlso would only work correctly if ExecuteMe had done some type of
action, then I would call ExecuteMeAlso from within ExecuteMe. (or look at
the possibility of refactoring my code)

Hope this helps.
"GVN" <mu***********@hotmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
>I thank both Duggi and Srinivas.

I found in both of your postings that:
"Delegates are often used for events where you don't know who else
might be interested in being notified. "

Can you explain me in detail about the above statement? I am still in a
dilemma about the above statement.
My dilemma is that:
The user will declare the skeleton of the delegate. He himself adds
some method(s) to the delegate. He will assign the delegate invocation
to some of the event. Then, why don't we know who else might be
interested in being notified?

Duggi wrote:
>Hi
As I see this you by calling method2 from method1,

1. generally delegates can be assigned methods from out side of the
class. as one can not expect how many of the methods would be assigned
to delegate (from client code of delegate) you can never keep on
calling one method from another and another and another.

2. I agree with Mattias point... myclass (that has delegate) will never
know who are interested in getting notified....

The obvious difference is that in the latter case, you have to know
that myMethod1 should call myMethod2. Delegates are often used for
events where you don't know who else might be interested in being
notified.

one can avoid these two points but that will introduce tight-coupling
between the classes of the application.

Thanks
-Srinivas.

GVN wrote:
Hi All,

I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:

I have a delegate myDelegate with object dlgt, and two methods
myMethod1(), and myMethod2().

Using multicast delegates definition I can write as follows to add the
two methods to my multicast delegate:

dlgt += new myDelegate(myMethod1);
dlgt += new myDelegate(myMethod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMethod1);

Inside my myMethod1() I will call myMethod2() method.

What is the difference among the above two cases?

Thanks in advance....
Nov 25 '06 #5
GVN
Chris,

Thanks a lot for your solution. Really I got a very good understanding
of delegates. Till now I was in ambiguity with my questions, and you
answered them.

Can I have your mail-id so that I can post my questions directly to
you?

Thanks,
GVN.

Chris wrote:
Think of the Button class. You typically need to tell the call what it
needs to do whenever it is clicked.

Button foo = new Button();
foo.Click += ExecuteMe(Object sender, EventArgs e);

The author of the button class has no idea what you are going to call - but
yet you can call whatever you like without changing any of the original
Button code.

So then the next question is why would you do

Button foo = new Button();
foo.Click += ExecuteMe(Object sender, EventArgs e);
foo.Click += ExecuteMeAlso(Object sender, EventArgs e);

instead of calling ExecuteMeAlso from inside ExecuteMe?

The answer is that it is a design choice. If the only thing that ExecuteMe
and ExecuteMeAlso have in common is that they both need to execute when the
button is clicked, I would keep them seperate. On the other hand, if
ExecuteMeAlso would only work correctly if ExecuteMe had done some type of
action, then I would call ExecuteMeAlso from within ExecuteMe. (or look at
the possibility of refactoring my code)

Hope this helps.
"GVN" <mu***********@hotmail.comwrote in message
news:11**********************@j72g2000cwa.googlegr oups.com...
I thank both Duggi and Srinivas.

I found in both of your postings that:
"Delegates are often used for events where you don't know who else
might be interested in being notified. "

Can you explain me in detail about the above statement? I am still in a
dilemma about the above statement.
My dilemma is that:
The user will declare the skeleton of the delegate. He himself adds
some method(s) to the delegate. He will assign the delegate invocation
to some of the event. Then, why don't we know who else might be
interested in being notified?

Duggi wrote:
Hi
As I see this you by calling method2 from method1,

1. generally delegates can be assigned methods from out side of the
class. as one can not expect how many of the methods would be assigned
to delegate (from client code of delegate) you can never keep on
calling one method from another and another and another.

2. I agree with Mattias point... myclass (that has delegate) will never
know who are interested in getting notified....

The obvious difference is that in the latter case, you have to know
that myMethod1 should call myMethod2. Delegates are often used for
events where you don't know who else might be interested in being
notified.

one can avoid these two points but that will introduce tight-coupling
between the classes of the application.

Thanks
-Srinivas.

GVN wrote:
Hi All,

I recently worked on delegates. But I have a question regarding
Multicast delegates. The scenario is as follows:

I have a delegate myDelegate with object dlgt, and two methods
myMethod1(), and myMethod2().

Using multicast delegates definition I can write as follows to add the
two methods to my multicast delegate:

dlgt += new myDelegate(myMethod1);
dlgt += new myDelegate(myMethod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMethod1);

Inside my myMethod1() I will call myMethod2() method.

What is the difference among the above two cases?

Thanks in advance....
Nov 28 '06 #6

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

Similar topics

0
by: IceShock | last post by:
I have been studying the C# language for sometime now and i have noticed something about delegates. When are single cast delegates to be used? even a better question: How do we define a singlecast...
7
by: Jim H | last post by:
Is there something special I need to do to send data to a multicast IP and have it go across a router? Router is a Win2000 Server connecting 2 networks via RRAS PPTP. The routing appears to be...
6
by: Andrew Hayes | last post by:
Having to remember to unregister for events to prevent a ref count seems to be quite a burden to place on a developer. I have reviewed the WeakMulticastDelegate solution proposed by Xavier Musy...
1
by: Christoph Nahr | last post by:
The task: I want to let a background thread and a Windows Forms foreground thread communicate via callback methods. Now synchronization of a *single* delegate works just fine with the usual...
1
by: Hiten | last post by:
Hi I have created multicast delegates, now i want to point to perticular reference from delegate to invoke that mathod how do i do that please any one know it..... Thanks --- Hitendra
7
by: pietro.cerutti | last post by:
Hi guys, I have a daemon running on Debian and listening for multicast packets sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries). The server is plugged into a VLAN trunk with eth0 and...
2
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
What are the different features (in VB.NET) between a) Custom Events and b) Multicast Delegates to raise events? On the bottom line my class ad a) manages a list of handlers or ad b) obtains a...
5
by: AliRezaGoogle | last post by:
Hi, I have a conceptual question on Events and Multicast Delegates. Let me explain: As we know an event is a multicast delegate. What we declare as an event is inherently a multicast delegate....
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.