locked
Custom Event handler is always null RRS feed

  • Question

  • I have the following code:

    // namespace test.events
    public class Car
    {  
        public delegate void ChangingHandler (object sender, EventArgs ca); 
        public event ChangingHandler Change;
            
        public Car() { }

        public void RaiseEvent()
        { 
           Change(this, new EventArgs()); 
         } 
    }

    //namespace test
    private void button1_Click(object sender, EventArgs e)
    {
            Car car=new Car();
            car.Change+=new Car.ChangingHandler(car_Change);
            car.RaiseEvent();
    }

    private void car_Change(object sender, EventArgs e)
    {
            MessageBox.Show("Event raised");
    }


    My problem is that when the event is raised and o come to the line  Change(this, new EventArgs()); in RaiseEvent the Change handler is always null.
    Why is that?

    Wednesday, July 15, 2009 6:22 AM

Answers

  • you had to make it static, because Main method is static.
    • Marked as answer by Harry Zhu Tuesday, July 21, 2009 8:52 AM
    Wednesday, July 15, 2009 8:29 AM

All replies

  • Hi Morten,

    I am unable to reproduce the issue as for me the event is raised as expected, ending with the MessageBox.  If the event is not raised for you, you most likely have some other code interfering.  Make sure the event subscription is added and not removed somewhere else.


    ---
    Happy Coding!
    Morten Wennevik [C# MVP]
    Wednesday, July 15, 2009 6:35 AM
  • You chould always check if someone has subscribed your event:
    public void RaiseEvent()
        { 
           if (Change != null) {
                    Change(this, new EventArgs()); 
            }
         } 

    But in general i've tested your Car class like this:
    static void Main(string[] args)
            {
                Car car = new Car();
                car.Change += new Car.ChangingHandler(car_Change);
                Console.ReadKey();
                car.RaiseEvent();
                Console.ReadKey();
            }
    
            private static void car_Change(object sender, EventArgs e)
            {
                Console.WriteLine("event raised");
            }
    And it worked.
    • Proposed as answer by dutzend Friday, June 8, 2018 5:58 PM
    Wednesday, July 15, 2009 6:42 AM

  • your code is running ok but you have to take care (if no one subscribed to the event and called the RaiseEvent method your application will break)

    in the RaiseEvent method before you fire the event you have to check if no one subscribed to it like this

        public void RaiseEvent()
        {
            if(Change != null)
                Change(this, new EventArgs());
        } 

    We are volunteers, if the reply help you mark it as your answer. thanks!!
    Wednesday, July 15, 2009 6:46 AM
  • Thanks for all your answers.

    Note that each program listing is defined in a different project.

    Here is the whole code

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ClassLibraryCar
    {
          public class
    Car
         { 
                public delegate void ChangingHandler (object sender, EventArgs ca);
                public event ChangingHandler Change;

                public Car() { }

                public void RaiseEvent()
                {
                      if (Change != null)
                           Change(this, new
    EventArgs()); 
                }
          }
    }

    AND another project

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ClassLibraryCar;

    namespace ConsoleApplicationEvent
    {
             class
    Program
             {
                   static void Main(string[] args)
                   {
                         Car car = new Car();
                         car.Change += new ClassLibraryCar.Car.ChangingHandler(car_Change);
                         car.RaiseEvent();
                   }

                   private static void car_Change(object sender, EventArgs e)
                  {
                         Console.WriteLine("Event raised");
                         Console.ReadLine();
                   } 
              }
    }

    Hmm now it seams to work i had to make the car_change static

    Wednesday, July 15, 2009 8:19 AM
  • you had to make it static, because Main method is static.
    • Marked as answer by Harry Zhu Tuesday, July 21, 2009 8:52 AM
    Wednesday, July 15, 2009 8:29 AM
  • Hi - I think my information can help.

    I changed your code, some small modifications.







    NOTE: YOU can have two classes in the same *.cs or project, but separate them using namespaces.

    I hope this information was helpful...

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Wednesday, July 15, 2009 10:22 AM