invoking base class event from the method of derived class method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • furqi
    New Member
    • Dec 2009
    • 21

    invoking base class event from the method of derived class method

    hi every body
    i write a code in c sharp in which i have made a base class and make an event there.Then i make a derived class and made an other event in that class.
    Now what i wanna do is that i wanna invoke in derived class method the base class event but i am facing an error there i am unable find the solution.canaby body tell me the solution.follow ing is the code.



    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace derivedevents
    {
        delegate void eventhandler();
        class bclass
        {
            public  event eventhandler handler;
            public void furqan()
            {
                Console.WriteLine("furqan");
            }
            public void furqan1()
            {
                Console.WriteLine("ahmed");
            }
            public void fire()
            {
                handler();
            }
    
        }
    }
    
    //now the base class
    namespace derivedevents
    {
        delegate void eventhandler1();
        class dclass:bclass 
        {
            public event eventhandler1 handler1;
            public void ahmed()
            {
                Console.WriteLine("ahmed");
            }
            public void ahmed1()
            {
                Console.WriteLine("ahmed ahmed");
            }
            public void fire1()
            {
                handler1();
                handler();\\[B] i am actually facing the problem here facing error[/B]       
            }
    
        
    
        
        }
    }
    Last edited by tlhintoq; Dec 23 '09, 09:50 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • RobertPerona
      New Member
      • Dec 2009
      • 2

      #3
      To fix your immediate problem you could change your fire1 method in derived class to the following.

      Code:
              public void fire1() 
              { 
                  handler1(); 
                  base.fire();         
              }
      I would recommend checking out the following link for more on the suggested form of events in c#
      MSDN: Raising Base Class Events
      MSDN: Event Design

      Comment

      • furqi
        New Member
        • Dec 2009
        • 21

        #4
        again error

        i have also used base keyword but it is also giving me an error
        any other suggestion please

        Comment

        • RobertPerona
          New Member
          • Dec 2009
          • 2

          #5
          Please post the error you are getting. Also any additional code, for example the code you are using to instantiate and call the class if you are getting a runtime exception.

          In addition for safety in case of execution of unattached events any code invoking the event should follow the following pattern, outlined in previous links I posted.

          Code:
                  
                  public void fire1()  
                  {  
                      if(handler1 != null)
                      {
                          handler1();  
                      }
                      base.fire();          
                  }
          Notes
          1. This would also apply to your fire method in the base class, test for handler to be null before invoking.
          2. Normally the event execution method would be named On<Event Name> in you code being onHandler.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Another example

            Comment

            Working...