tags:

views:

53

answers:

2

I'm trying to expose some events from a private object which is contained inside the object I am creating and it looks like the compiler is happy with this:

private WindowUpdateServer _windowUpdateServer;

public event WindowUpdateHandler WindowUpdated;

public RecieveWindowFramesManager() {

    _windowUpdateServer = new WindowUpdateServer();

    _windowUpdateServer.ExistingWindowUpdated += WindowUpdated; // ExistingWindowUpdated is a WindowUpdateHandler
}

But after RecieveWindowFramesManager is initialized _windowUpdateServer.ExistingWindowUpdated == null.

Am I missing something here - it seems this should work?

It is worth noting that after RecieveWindowFramesManager is initialized I attach an event listener to WindowUpdated but it never gets called (even though _windowUpdateServer.ExistingWindowUpdated gets fired).

+6  A: 

I'm not sure, but I think this only assigns those handlers from the WindowUpdated event, that were set when the += operation took place. Since it is a constructor, the list was empty. To do what you want, create your own event handler for ExistingWindowUpdated and fire the WindowUpdated event from there.

Vilx-
Perfect, thank you for the good explanation.
CuriousCoder
Thanks again for your explanation. ArildF's solution works very well in my situation - so I have accepted his answer as more fitting.
CuriousCoder
+4  A: 

I'd do something like this:

public event WindowUpdateEventHandler WindowUpdated
{ 
    add{ _windowUpdateServer.ExistingWindowUpdated += value; }
    remove{ _windowUpdateServer.ExistingWindowUpdated -= value; }
}

It has some issues if you're actually using the sender argument in the event handlers, though; it'll point to the inner object rather than the one you're getting the events from.

ArildF
Nice! More elegant even then my solution!
Vilx-
I just tried this out and it works perfectly. Really nice solution.
CuriousCoder