EventRaise and "pass thru"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sloan

    EventRaise and "pass thru"


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

    I am using the Adapter Pattern to sync up some different interfaces.
    Learn how to use the C# Adapter design pattern to make incompatible interfaces work together, with quick and easy examples. 100% Source code.



    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.




  • Ben Voigt

    #2
    Re: EventRaise and "pass thru"

    "sloan" <sloan@ipass.ne t> wrote in message
    news:%23tB90m4b GHA.1264@TK2MSF TNGP05.phx.gbl. ..[color=blue]
    >
    > 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.
    >[/color]

    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).
    [color=blue]
    >
    > 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.
    >
    >
    >
    >[/color]


    Comment

    • Marc Gravell

      #3
      Re: EventRaise and &quot;pass thru&quot;

      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 SomeEventHandle r SomeEvent {
      add {innerObject.So meEvent+=value; }
      remove {innerObject.So meEvent-=value;}
      }

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

      Marc


      Comment

      • sloan

        #4
        Re: EventRaise and &quot;pass thru&quot;


        I got it, thanks.

        The part I was missing was

        //ClassB code
        protected virtual void OnInfoMessage(o bject sender, SqlInfoMessageE ventArgs
        e)
        {
        if (null!=InfoMess age ) {
        InfoMessage(thi s, e);
        }
        }



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

        // in class declarations of ClassB
        public event SqlInfoMessageE ventHandler InfoMessage;




        //method which wires up the ClassA event, to pass thru ClassB//this code is
        in ClassB
        private void WireUpPassThruE vent(SqlConnect ion sc)
        {
        if (null!=sc)
        {
        sc.InfoMessage += new EventHandler(th is.OnInfoMessag e);
        }
        }





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





        "Ben Voigt" <xyz> wrote in message
        news:e8VW3r4bGH A.1208@TK2MSFTN GP02.phx.gbl...[color=blue]
        > "sloan" <sloan@ipass.ne t> wrote in message
        > news:%23tB90m4b GHA.1264@TK2MSF TNGP05.phx.gbl. ..[color=green]
        > >
        > > 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,[/color][/color]
        without[color=blue][color=green]
        > > any knowledge (necessarily) that ClassB exists.
        > >[/color]
        >
        > Declare a matching event in ClassB (same delegate type). Handle the[/color]
        ClassA[color=blue]
        > event in a ClassB instance member. Fire ClassB's event using the same
        > EventArgs-derived parameter. Change the object sender parameter to this[/color]
        if[color=blue]
        > 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).
        >[color=green]
        > >
        > > I know how to subscribe to Events, but I don't know how to "pass the[/color][/color]
        event[color=blue][color=green]
        > > thru" in this scenario.
        > >
        > >
        > > If I need to provide more concrete information, then let me know.
        > >
        > >
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...