views:

71

answers:

3

I have a solution which contains 3 project. One project handles asynchronous communinications. When it has completed it's callback, it raises an event SomethingCompleted. How do I subscribe to this event from another project in the same solution?

I have the event handlers built in the receiving project but it does not see the event in the sending project.

+5  A: 

There's nothing special about events raised in a separate assembly, unless the event itself is declared with the internal access modifier. Check that it's public.

To give an example of what I mean, I'm sure you don't think twice about subscribing to Button.Click - but Button is in a different assembly, isn't it?

Jon Skeet
A: 

You need to have a reference to the class raising the event to register an event handler.

Once you have that, it's done like registering any other event handler:

foo.SomethingCompleted += (sender, e) => this.DoSomething();
Mark Seemann
Thanks for your help, that's what I needed
Congero
+1  A: 

The event must be declared as public in the class which defines it:

public class Something
{
  public event EventHandler SomethingCompleted;
}

You can then subscribe to it just as to any other event:

Something s = ...;
s.SomethingCompleted += SomeEventHandler;
itowlson