473,410 Members | 1,904 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,410 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 1731
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: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.