473,498 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

coordinating the creation of delegate and event properties

If I have a class that exposes a delegate (see following code), how do
other objects who wish to receive notifications be coded? What I mean
is the first object to receive notifications must create the delegate
by doing a 'new' whereas other objects just need to use += to add to
the delegate list. Is there a way of doing this without each class
testing whether to create the delegate first?
class ClassX
{
public delegate void MyDelegate(string x);
public MyDelegate OnDelegate;

...
}

class ReceivingDelegateNotifications
{
ClassX.OnDelegate = new ClassX.MyDelegate(MyMethod);
ClassX.OnDelegate += new ClassX.MyDelegate(MySecondMethod);

private void MyMethod(string y) {...}
private void MySecondMethod(string y) {...}
}

Thanks in advance,
Darren.
Nov 16 '05 #1
1 1138
Since you have multple listeners, you could use an event instead of a
delegate instance, which doesnt require to be initialized when the first
object registers it's method.

"Darren" wrote:
If I have a class that exposes a delegate (see following code), how do
other objects who wish to receive notifications be coded? What I mean
is the first object to receive notifications must create the delegate
by doing a 'new' whereas other objects just need to use += to add to
the delegate list. Is there a way of doing this without each class
testing whether to create the delegate first?
class ClassX
{
public delegate void MyDelegate(string x);
public MyDelegate OnDelegate;

...
}

class ReceivingDelegateNotifications
{
ClassX.OnDelegate = new ClassX.MyDelegate(MyMethod);
ClassX.OnDelegate += new ClassX.MyDelegate(MySecondMethod);

private void MyMethod(string y) {...}
private void MySecondMethod(string y) {...}
}

Thanks in advance,
Darren.

Nov 16 '05 #2

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

Similar topics

4
2156
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
6
4811
by: Ondrej Sevecek | last post by:
Hello, what is the difference between "event" and the only use of delegate? Why one should note events with "event" keyword when the functionality seems the same as with pure delegates? When I...
2
1120
by: Matt | last post by:
Can anyone point me in the direction of creating an event in a class I develop? What I am looking for is finding a way that when a property gets set or is changed a method is automatically run to...
3
1108
by: Jon Turner | last post by:
I have an asynchronous call to a remote procedure that is invoked thru BeginInvoke. The problem is that in the Delegate if I throw an event before the CallBack function exits, the CallBack will...
1
1308
by: Greg Wilkerson | last post by:
I hope I can explain this in a way that can be understood. I have a custom control, I'll call it navigator, used to navigate "things". I has, among other controls, two standard buttons, previous...
7
1765
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
7
3430
by: sam.m.gardiner | last post by:
I'm working with VB.NET events and I want a way to disconnect all the handlers of an event. I want to do this in the object that is the source of the event. This is slightly tricky in VB.Net as the...
3
11537
by: steve | last post by:
Hi all, I want to be able to hirearchally define event delegate (declarations) WITHIN interfaces. Unfortunatelly C# 1 doesn't appear to support it. How does the following look as a...
5
8890
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....
0
7002
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
7165
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,...
0
7205
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...
1
6887
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...
0
7379
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...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
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 ...

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.