Base and derived event firing in .NET v1

P

pkiddie

Hi!

Im having real issues understanding delegates/events in .NET v1, and was
hoping someone could shed some light on where I'm not quite understanding.
I've got this piece of code in a base user control, which i've called
'PropertyPanel'. In it is the following code:

public delegate void EventHandler(object sender, EventArgs e);

public event EventHandler AppliedProperties;

private void buttonOK_Click(object sender, System.EventArgs e)

{

EventHandler handler = AppliedProperties;

EventArgs ex = new EventArgs();

if (ValidateForm())

{

if (handler!=null)

{

handler(this, ex);

}

}

}

Now what I'd like to do is to derive from my base user control several other
property panels, but still hook to my AppliedProperties event handler within
the main form, which will fire from a specific implementation within the
derived class, for example by:

(in the main form)

PropertyPanel myPropertyPanel;

myPropertyPanel = new NodePropertyPane();

myPropertyPanel.AppliedProperties += new EventHandler(this.Test);



OR myPropertyPanel = new LinkPropertyPane();

myPropertyPanel.AppliedProperties += ....



Does anyone know or can give me advice how to best go about this?

Any help would be greatly appreciated,

Paul
 
N

Nicholas Paldino [.NET/C# MVP]

pkiddie,

The problem here is that you are defining the EventHandler delegate in
your class. You also subsequently declare your event of that type. There
are two ways to fix this. The first is to patch up the call site for the
event:

myPropertyPanel.AppliedProperties += new
PropertyPanel.EventHandler(this.Test);

Of course, this isn't the best way to go about this. Because
EventHandler is already defined, I would just remove the delegate
declaration (but keep the event declaration). Then, your code should be
fine.

Hope this helps.
 

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