473,671 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(myMe thod1);
dlgt += new myDelegate(myMe thod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMe thod1);

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 3873
>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(myMe thod1);
dlgt += new myDelegate(myMe thod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMe thod1);

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(myMe thod1);
dlgt += new myDelegate(myMe thod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMe thod1);

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(Objec t 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(Objec t sender, EventArgs e);
foo.Click += ExecuteMeAlso(O bject 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.comwrot e in message
news:11******** **************@ j72g2000cwa.goo glegroups.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(myMe thod1);
dlgt += new myDelegate(myMe thod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMe thod1);

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(Objec t 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(Objec t sender, EventArgs e);
foo.Click += ExecuteMeAlso(O bject 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.comwrot e in message
news:11******** **************@ j72g2000cwa.goo glegroups.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(myMe thod1);
dlgt += new myDelegate(myMe thod2);

Can I write the same as follows?

I will create a single cast delegate:

dlgt = new myDelegate(myMe thod1);

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
3933
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 delegate? The thing is I have been using the .net v 1.1 2003 frame work and i have been told that this might be the issue. I have been told that in this new framework the SingleCast delegates are abandoned and that everythin now is
7
7703
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 working because I can ping the multicast address and get responses from the server on the other network. I didn't think ping would work like that but as long as I have the server process running I can ping the multicast IP and get a response. The...
6
3457
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 (http://www.seedindustries.com/blog/x/2004_06_01_archive.html) and Greg Schechter’s Avalon solution. 1) Is there a more general solution in C#? 2) Why are delegates not weakreferences? It would seem a logical decision to make - if the only...
1
3390
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 Control.Invoke or Control.BeginInvoke methods. However, I couldn't find any information on using this technique with multicast delegates, i.e. the magic thingie created by the "event" keyword in C#.
1
1193
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
8450
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 joins several VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to capture the UDP packets on any interfaces, so it spawns a thread for each interface specified in a config file, and for each thread it creates a socket: ...
2
1758
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 list of handlers (using GetInvocationList) and invokes them one-by-one. So where is the difference? eg are Custom Events the only way to raise an event asynchronously? thanks herbert
5
8916
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. I really do not undrestand what additional features the "event" keyword adds to multicast delegate. The only thing that I see as an additional feature is a "Thunder Icon" near event name in VS IDE when intellisense works;).
9
3107
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 to store multicast delegates in a hash table, and then fire the delegates one of two ways (after registering/ creating the delegates, etc).
0
8402
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8605
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8676
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.