473,238 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Event = null;

I need to remove all handlers from a particular event. I am doing this
in the class where the event is defined.

I have this vague memory that I can say:

this.MyEvent = null;

and this will clear all delegates from the event, but I'm not sure, and
I can't find the relevant documentation.

Can I do that, or do I have to loop through each delegate like this:

foreach (Delegate d in this.MyEvent.GetInvocationList())
{
this.MyEvent -= d;
}

?

Dec 5 '05 #1
3 1725
Bruce Wood <br*******@canada.com> wrote:
I need to remove all handlers from a particular event. I am doing this
in the class where the event is defined.

I have this vague memory that I can say:

this.MyEvent = null;

and this will clear all delegates from the event, but I'm not sure, and
I can't find the relevant documentation.
Yes, you can - *if* the event has been declared as a "field-like" event
within the same class.
Can I do that, or do I have to loop through each delegate like this:

foreach (Delegate d in this.MyEvent.GetInvocationList())
{
this.MyEvent -= d;
}


Basically, a "field-like" event is actually two things:
1) The event (a pair of add/remove methods)
2) A delegate variable

You can do what you like to a delegate variable, but the only things
you can do with an event are add and remove handlers (and fire the
event, in theory, but C# doesn't implement that part itself).

What you *can't* do is clear an event declared in another class (even a
base class) with your first try - nor can you get the invocation list.
From the point of view of any other class (at compile-time; reflection
is a different story) the event is *just* an event.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 5 '05 #2
So, just to clarify, if I have within my class:

protected event System.EventHandler UpdateBeginning;
protected event System.EventHandler UpdateEnded;

then I can do this:

protected void BeginUpdate()
{
if (this.UpdateBeginning != null)
{
this.UpdateBeginning(this, System.EventArgs.Empty);
}
this.UpdateEnded = null;
this._updateInProgress = true;
}

and the this.UpdateEnded = null will clear all events from the
"UpdateEnded" delegate?

Dec 5 '05 #3
That's correct.

Within your class, "UpdateEnded" refers to a compiler-created field
whose type is EventHandler, so you can set it to null and forget all
the delegates that have been added. It's only outside your class that
UpdateEnded refers to an event (and thus can only be used with += and
-=).

Jesse

Dec 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate,...
5
by: Action | last post by:
does it works like ordinary virtual method?? coz I find that child class can't invoke the event of the parent class. class parent { public virtual event SomeDelegate SomeChanged; } class...
2
by: Mark | last post by:
I'm trying to raise and handle an event. I'm using an example from the VS.NET help files (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht...
1
by: tony | last post by:
Hello!! Hello Victor! I use a product called flygrid to create grid tables. In many of my forms I create such grid tables. Some columns in these grid tables is of type drop down list where I...
14
by: Brett Romero | last post by:
I'm using a DataGrid and have assigned this.DataSourceChanged += new EventHandler( DataGrid_DataSourceChanged ); This works fine but there is one case where it doesn't. How can I check if...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
0
by: Matt Michler | last post by:
I have a WebUserControl with a custom event, when this event is fired, the delegate or event object is throwing a null reference exception. The odd part of this is, it used to work flawlessly, now...
5
by: nt5515 | last post by:
im trying to write a program that store a binary tree of possible events in an array. i need to be able to sort the the Events in the array based on the previous event that caused it by the time...
6
by: kirk | last post by:
I have three events, using event handler methods as depicted below. Two of those event handler methods need to reset specific data whenever the other event left fires. I wasn't sure how to...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.