Skip to content

Fire an event from the constructor ???

Paw

Asked Oct 24, 2006, 3:39 AM EDT3 answers
Share
LinkedInX

Why cant i fire an event from the constructor of an class ??

If i use this code:
MyErgumentCollection ca = new MyErgumentCollection("Object createt @ " + DateTime.Now);
OnFireEventCall(this, ca);
in the constructor public ClassWithEvent()
i get an error, but if i remove it from public ClassWithEvent() and use the void FireEvent() insted, then it works fine ????

See the code below for more details...

I Need help!!!!!!!!!


namespace MyClassWithEvent
{
      //Class with arguments...
      public class MyErgumentCollection : System.EventArgs
      {
            private string message;

            public MyErgumentCollection(string m)
            {
                  this.message = m;
            }

            public string Message()
            {
                  return message;
            }
      }


      //Class with test functions...
      public class ClassWithEvent
      {
            public delegate void FiringEvent(object sender, MyErgumentCollection ca); //Declare delegate
            public event FiringEvent OnFireEventCall; //Declare Event name

            public ClassWithEvent()
            {
                  MyErgumentCollection ca = new MyErgumentCollection("Object createt @ " + DateTime.Now); //Create Argument
                  OnFireEventCall(this, ca); //Fire event, This line returns the following error: Object reference not set to an instance of an object.
            }

            public void FireEvent()
            {
                  MyErgumentCollection ca = new MyErgumentCollection("Event Fired @ " + DateTime.Now); //Create Argument
                  OnFireEventCall(this, ca); //Fire event
            }
      }
}

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    The variable OnFireEventCall is declared but was never initialized, that's why you're getting the error.

          public class ClassWithEvent
          {
                public delegate void FiringEvent(object sender, MyErgumentCollection ca); //Declare delegate
                public event FiringEvent OnFireEventCall; //Declare Event name

                public ClassWithEvent()
                {
                      MyErgumentCollection ca = new MyErgumentCollection("Object createt @ " + DateTime.Now); //Create Argument
                      OnFireEventCall(this, ca); //Fire event, This line returns the following error: Object reference not set to an instance of an object.
                }

                public void FireEvent()
                {
                      MyErgumentCollection ca = new MyErgumentCollection("Event Fired @ " + DateTime.Now); //Create Argument
                      OnFireEventCall(this, ca); //Fire event
                }
          }
    0
  • 0
    Thanks for the reply Aleksey Nagoga

    So how do i initialized the OnFireEventCall before using it ????
    0
  • 0
    Problem solved!!!

    I simply throw the handler with the constructor like this.

    public ClassWithEvent(FiringEvent e)
    {
        OnFireEventCall += e;
        MyErgumentCollection ca = new MyErgumentCollection ("Object createt @ " + DateTime.Now); //Create Argument
        OnFireEventCall(this, ca); //Fire event, This line returns the following error: Object reference not set to an instance of an object.
    }

    0