Hello All,
I am trying to derive form CancelEventArgs, but keep getting a NullReferenceException when I try to use it. I think I'm missing something basic, but darned if I can find it.
Here are the snippets from a project I created to demonstrate the problem:
Code:
// Derived Class
public class MyCancelEventArgs : CancelEventArgs
{
private double m_OldValue = 0;
private double m_NewValue = 0;
private int m_Index = 0;
public double OldValue
{
get { return m_OldValue; }
}
public double NewValue
{
get { return m_NewValue; }
}
public int Index
{
get { return m_Index; }
}
public MyCancelEventArgs(double OldVal, double NewVal, int Index) : base()
{
m_OldValue = OldVal;
m_NewValue = NewVal;
m_Index = Index;
this.Cancel = false;
}
}
public delegate void CancelEventDelegate(object sender, MyCancelEventArgs e);
public class Form1 : System.Windows.Forms.Form
{
public static event CancelEventDelegate CancelEvent;
double MyValue = 0.0;
int CancelCounter = 0;
private System.Windows.Forms.Button button1;
protected virtual void OnCancelEvent(MyCancelEventArgs e)
{
CancelEvent(this, e);
}
private void button1_Click(object sender, System.EventArgs e)
{
double OldValue = MyValue;
MyValue++;
CancelCounter++;
MyCancelEventArgs NewCancelEventArgs = new MyCancelEventArgs(OldValue, MyValue, CancelCounter);
OnCancelEvent(NewCancelEventArgs);
if (! NewCancelEventArgs.Cancel)
{
lblCancelEvent.Text = "Cancel Event #" + NewCancelEventArgs.Index + " Accepted at " + DateTime.Now.ToLongTimeString();
}
else
{
lblCancelEvent.Text = "Cancel Event #" + NewCancelEventArgs.Index + " Cancelled at " + DateTime.Now.ToLongTimeString();
MyValue--;
}
}
} // End of Form1 class
I believe that I have included all the pieces related to the CancelEventArgs stuff I've added. The red stuff is specific to the CancelEventArgs, and the bold line is the one that is failing.
All the usual form initialization code, etc., is left out for clarity.
If somebody could tell me what I'm doing wrong, I'd certainly appreciate it.
Thanks,
pagates