class inherit question

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

    #1

    class inherit question

    Hi,
    I have two classes (ClassA and ClassB),

    public ClassA {
    public ClassA() {
    this.MyButton.C licked += new
    System.EventHan dler(this.MyBut ton_Click);
    }

    private void MyButton_Click( object sender, System.EventArg s e) {
    MyFunc();
    }

    protected void MyFunc() {
    }
    }


    public ClassB : ClassA {
    .... some functions
    }

    What I need is, once the event is fired in ClassA, how can I make
    inherited classes (e.g ClassB) to know base class fires an event?

    --
    WWW: http://hardywang.1accesshost.com
    ICQ: 3359839
    yours Hardy


  • Joe

    #2
    Re: class inherit question

    Add a method called OnClick to class A. In the event handler, call this
    method. In class B just override the method. Make sure class B calls
    base.OnClick

    Hope this helps.

    -Joe

    "Hardy Wang" <hardywang@news groups.nospam> wrote in message
    news:u2J%23m4tG GHA.216@TK2MSFT NGP15.phx.gbl.. .[color=blue]
    > Hi,
    > I have two classes (ClassA and ClassB),
    >
    > public ClassA {
    > public ClassA() {
    > this.MyButton.C licked += new
    > System.EventHan dler(this.MyBut ton_Click);
    > }
    >
    > private void MyButton_Click( object sender, System.EventArg s e) {
    > MyFunc();
    > }
    >
    > protected void MyFunc() {
    > }
    > }
    >
    >
    > public ClassB : ClassA {
    > .... some functions
    > }
    >
    > What I need is, once the event is fired in ClassA, how can I make
    > inherited classes (e.g ClassB) to know base class fires an event?
    >
    > --
    > WWW: http://hardywang.1accesshost.com
    > ICQ: 3359839
    > yours Hardy
    >[/color]


    Comment

    • Randy A. Ynchausti

      #3
      Re: class inherit question

      Hardy,
      [color=blue]
      > Add a method called OnClick to class A. In the event handler, call this
      > method. In class B just override the method. Make sure class B calls
      > base.OnClick
      >[color=green]
      >> What I need is, once the event is fired in ClassA, how can I make
      >> inherited classes (e.g ClassB) to know base class fires an event?[/color][/color]

      Although Joe provided a good answer to the question, "How does one invoke
      the event handler of a superclass from one of its subclasses?", I believe
      your question was, "How does one invoke the event handler of all subclasses
      from one of its superclasses?" This is an interesting question and has many
      complications. However, the psuedo code might look like this:

      1. If the interesting event handler is triggered on an instance of ClassA
      continue to step 2; otherwise exit
      2. Use reflection to traverse the hierarchy of ClassA to find all classes
      that inherit from ClassA
      3. Find all instances of all of the classes found in step 2
      4. Invoke the interesting event handler for each of the instances found in
      step 3.

      I thought I would respond with this much information to see if I understand
      your question. We can discuss ways to accomplish each of these steps in
      follow-up responses, if necessary.

      Regards,

      Randy


      Comment

      • MyName

        #4
        Re: class inherit question

        > 1. If the interesting event handler is triggered on an instance of ClassA[color=blue]
        > continue to step 2; otherwise exit
        > 2. Use reflection to traverse the hierarchy of ClassA to find all classes
        > that inherit from ClassA
        > 3. Find all instances of all of the classes found in step 2
        > 4. Invoke the interesting event handler for each of the instances found in
        > step 3.
        >[/color]

        4a. Check somehow if instance is registered to that particular event.


        Comment

        • Yuan Ren[MSFT]

          #5
          RE: class inherit question

          Hi Hardy,

          Welcome to MSDN newsgroup!

          As Randy's suggestion if you want to let the child class knows the base
          class event firing, you will use reflection in the runtime. Then, you can
          use the iteration to get all child classes and fire a new event in the
          child class.

          Regards,

          Yuan Ren [MSFT]
          Microsoft Online Support

          Comment

          Working...