EventRaise and "pass thru"

  • Thread starter Thread starter sloan
  • Start date Start date
S

sloan

How does one "pass thru" a Raised Event....

I am using the Adapter Pattern to sync up some different interfaces.
http://www.dofactory.com/Patterns/PatternAdapter.aspx


My Question is this:



ClassA raises an event.


I have a ClassB which is using ClassA , but does not expose it in anyway
(aka, no public property exposing the instanitated ClassA). (for reasons
like the pattern above)


ClassC is using ClassB.


When an event in ClassA is raised, I want ClassC to know about it, without
any knowledge (necessarily) that ClassB exists.


I know how to subscribe to Events, but I don't know how to "pass the event
thru" in this scenario.


If I need to provide more concrete information, then let me know.
 
sloan said:
How does one "pass thru" a Raised Event....

I am using the Adapter Pattern to sync up some different interfaces.
http://www.dofactory.com/Patterns/PatternAdapter.aspx


My Question is this:



ClassA raises an event.


I have a ClassB which is using ClassA , but does not expose it in anyway
(aka, no public property exposing the instanitated ClassA). (for reasons
like the pattern above)


ClassC is using ClassB.


When an event in ClassA is raised, I want ClassC to know about it, without
any knowledge (necessarily) that ClassB exists.

Declare a matching event in ClassB (same delegate type). Handle the ClassA
event in a ClassB instance member. Fire ClassB's event using the same
EventArgs-derived parameter. Change the object sender parameter to this if
you want to hide the internal ClassA object, pass through the original
sender to make ClassB transparent (although the receiver now gets a
reference to your private ClassA object).
 
If you don't mind exposing the original sender, you can short-cut this; you
still need the matching declarations, but you don't need to catch and raise
at B:

public event SomeEventHandler SomeEvent {
add {innerObject.SomeEvent+=value;}
remove {innerObject.SomeEvent-=value;}
}

This way B doesn't ever get involved, other than as a means of hooking up to
A

Marc
 
I got it, thanks.

The part I was missing was

//ClassB code
protected virtual void OnInfoMessage(object sender, SqlInfoMessageEventArgs
e)
{
if (null!=InfoMessage ) {
InfoMessage(this, e);
}
}



The other parts (for completeness of the post) (These are coding items in
ClassB)

// in class declarations of ClassB
public event SqlInfoMessageEventHandler InfoMessage;




//method which wires up the ClassA event, to pass thru ClassB//this code is
in ClassB
private void WireUpPassThruEvent(SqlConnection sc)
{
if (null!=sc)
{
sc.InfoMessage += new EventHandler(this.OnInfoMessage);
}
}





The only part not here .. is the delegate, because I already had one defined
in the Sql class.
 

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

Back
Top