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
}
}
}
Loading
PawPosted Oct 25, 2006, 7:02 AM
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.
}
PawPosted Oct 25, 2006, 5:04 AM
So how do i initialized the OnFireEventCall before using it ????
Aleksey NagogaPosted Oct 24, 2006, 5:57 PM
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
}
}