Event Problem

G

Guest

I want use a different signature for a base class event and also override the
On... protected method. It's not working. What am I doing wrong? Here's
the code.

public partial class UCBase : UserControl
{
public event EventHandler IWasClicked;

public UCBase()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.OnIWasClicked(EventArgs.Empty);
}

protected virtual void OnIWasClicked(EventArgs e)
{
if (this.IWasClicked != null)
this.IWasClicked(this, e);
}
}


public partial class UCSpecial : UCBase
{

public new event CancelEventHandler IWasClicked;

public UCSpecial()
{
InitializeComponent();
}

protected override void OnIWasClicked(EventArgs e)
{
this.OnIWasClicked(CancelEventArgs.Empty);
}

protected virtual void OnIWasClicked(CancelEventArgs e)
{
MessageBox.Show("UCSpecial before raising the event.");

if (this.IWasClicked != null)
this.IWasClicked(this, e);
}
}
 
L

Larry Lard

Willy_At_Work said:
I want use a different signature for a base class event and also override the
On... protected method. It's not working. What am I doing wrong? Here's
the code.
protected override void OnIWasClicked(EventArgs e)
{
this.OnIWasClicked(CancelEventArgs.Empty); // here
}

protected virtual void OnIWasClicked(CancelEventArgs e)
{
MessageBox.Show("UCSpecial before raising the event.");

if (this.IWasClicked != null)
this.IWasClicked(this, e);
}

The problem is that CancelEventArgs.Empty is an EventArgs, not a
CancelEventArgs. So the line I have commented 'here' just calls itself
until the stack overflows.
 
L

Larry Lard

Larry said:
The problem is that CancelEventArgs.Empty is an EventArgs, not a
CancelEventArgs. So the line I have commented 'here' just calls itself
until the stack overflows.

I should add, the fix is to change the commented line to

this.OnIWasClicked(new CancelEventArgs());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top