Paw

Paw

  • NA
  • 5
  • 0

Fire an event from the constructor ???

Oct 24 2006 7:39 AM

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
            }
      }
}


Answers (3)