Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 8th, 2005, 12:47 PM
Authorized User
Join Date: Jun 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deriving from CancelEventArgs

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:

// 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;

        MyCancelEventArgs NewCancelEventArgs = new MyCancelEventArgs(OldValue, MyValue, CancelCounter);

        if (! NewCancelEventArgs.Cancel)
            lblCancelEvent.Text = "Cancel Event #" + NewCancelEventArgs.Index + " Accepted at " + DateTime.Now.ToLongTimeString();
            lblCancelEvent.Text = "Cancel Event #" + NewCancelEventArgs.Index + " Cancelled at " + DateTime.Now.ToLongTimeString();

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

Old April 9th, 2005, 10:29 AM
Friend of Wrox
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

you should first check the event for null(maybe the user hasn't attach any delegate to your event)
  CancelEvent(this, e);
  why your event is static?it should be nonstatic so that in different forms could be used.

software student.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Deriving an Autonumber field paulkholt Pro VB Databases 0 August 20th, 2004 10:51 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.