coordinating the creation of delegate and event properties


D

Darren

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.
 
Ad

Advertisements

G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top