By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,991 Members | 1,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,991 IT Pros & Developers. It's quick & easy.

PLS HELP! Override default event behavior (add/remove)

P: n/a
Hi,

I need to do some custom processing of event handlers assigned to a
particular event in my user control, e.g. when you add a handler it
should first remove it to make sure any given handler is not added twice
thruout the application. I was told i can use declaration of an event in
the form:

public event SelectedValueChangedHandler SelValueChanged
{
add
{
...
}
remove
{
...
}
}

but how do i really use it? Could anyone please give an example?
Amazingly google didn't show almost any info on that. Is it uncommon to
override add/remove for events?

Any ideas/comments are highly appreciated!

Thank you,
MuZZy
Apr 4 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Muzzy,

hope the following code snippet helps you understand it.

George

using System;
using System.ComponentModel;

public class EventListDemo
{
private EventHandlerList eventList = new EventHandlerList();
private static object somethingHappenedEvent = new object();

public event EventHandler SomeThingHappened
{
add { eventList.AddHandler(somethingHappenedEvent, value); }
remove { eventList.RemoveHandler(somethingHappenedEvent, value); }
}
}

Apr 4 '06 #2

P: n/a
Hi,
Personally I have never done it, but I know other people do it ( like Jon
Skeet) he has said in several thread that he overwrite it.
The reason is that the default implementation block on this : lock(this) and
this is not the best approach in multithreading. IIRC he does explain it
furhter in his site

Remember to use a locking statement to prevent concurrency problems
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"MuZZy" <tn*@newsgroups.nospam> wrote in message
news:Oy****************@TK2MSFTNGP09.phx.gbl...
Hi,

I need to do some custom processing of event handlers assigned to a
particular event in my user control, e.g. when you add a handler it should
first remove it to make sure any given handler is not added twice thruout
the application. I was told i can use declaration of an event in the form:

public event SelectedValueChangedHandler SelValueChanged
{
add
{
...
}
remove
{
...
}
}

but how do i really use it? Could anyone please give an example?
Amazingly google didn't show almost any info on that. Is it uncommon to
override add/remove for events?

Any ideas/comments are highly appreciated!

Thank you,
MuZZy

Apr 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.