473,395 Members | 1,556 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,395 software developers and data experts.

Qn: Adding and removing event delegates

Hi all

If I add multiple references to the SAME delegate in an event object..
How many references are stored in the delegate list managed by the object.

eg.

if I ..

SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;

And then

SomeObject.SomeEvent -= TotherObject.SomeEventHandler;

Will SomeObject.SomeEvent contain a reference to TotherObject.SomeEventHandler

// On a second note, Sorry I know this is not the place

Can someone point me to the *best* WinCE newsgroup focused on C++ dev for
user mode apps using eVC++?

Cheers

Steve
Nov 17 '05 #1
3 1657
Hi Steve,
How many references are stored in the delegate list managed by the object.
eg.
if I ..
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler; Five. When invoking the even, the five methods will in invoked in the
order you added them.
And then
SomeObject.SomeEvent -= TotherObject.SomeEventHandler;
Will SomeObject.SomeEvent contain a reference to >TotherObject.SomeEventHandler

The last TotherObject.SomeEventHandler will be remove. You still have
four!

Regards,
Thi

Nov 17 '05 #2
Thanks Truong,

IMHO it's a pity that an event calls the same delegate multiple times. It
would be nice if the delegate was only added once to the list managed by the
event object.

Regards

Steve

"Truong Hong Thi" wrote:
Hi Steve,
How many references are stored in the delegate list managed by the object.
eg.
if I ..
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;
SomeObject.SomeEvent += TotherObject.SomeEventHandler;

Five. When invoking the even, the five methods will in invoked in the
order you added them.
And then
SomeObject.SomeEvent -= TotherObject.SomeEventHandler;
Will SomeObject.SomeEvent contain a reference to >TotherObject.SomeEventHandler

The last TotherObject.SomeEventHandler will be remove. You still have
four!

Regards,
Thi

Nov 17 '05 #3
"steve" <st***@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
Thanks Truong,

IMHO it's a pity that an event calls the same delegate multiple times. It
would be nice if the delegate was only added once to the list managed by
the
event object.

Regards

Steve

"Truong Hong Thi" wrote:
Hi Steve,
>How many references are stored in the delegate list managed by the
>object.
>eg.
>if I ..
>SomeObject.SomeEvent += TotherObject.SomeEventHandler;
>SomeObject.SomeEvent += TotherObject.SomeEventHandler;
>SomeObject.SomeEvent += TotherObject.SomeEventHandler;
>SomeObject.SomeEvent += TotherObject.SomeEventHandler;
>SomeObject.SomeEvent += TotherObject.SomeEventHandler;

Five. When invoking the even, the five methods will in invoked in the
order you added them.
>And then
>SomeObject.SomeEvent -= TotherObject.SomeEventHandler;
>Will SomeObject.SomeEvent contain a reference to
> >TotherObject.SomeEventHandler

The last TotherObject.SomeEventHandler will be remove. You still have
four!

Regards,
Thi


You can achieve this behavior yourself by overriding the event registration
and unregistration and only adding a particular target once.

private EventHandler myEvt;
public event EventHandler MyEvt
{
add
{
if( myEvt != null )
{
bool found = false;
foreach( EventHandler evt in myEvt )
{
if( evt.Equals(value) )
{
found = true;
break;
}
}
if( !found )
{
myEvt += value;
}
}
}
remove
{
myEvt -= value;
}
}

Caveat: I wrote the code in Outlook Express - but it should give you the
general idea.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk?
Nov 17 '05 #4

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

Similar topics

5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
2
by: Lore Leuneog | last post by:
Hi. You can add new delegates for an event with += and you can remove delegates from an event by using: -= but is there a way (method) to list all delegates added to one event??? Sincerely...
3
by: Bob | last post by:
C# newbie here.... studying about delegates and now events. I just don't get the purpose of declaring events using the event keyword. What does this do for me that can't be done using only...
4
by: Bob Rock | last post by:
Hello, I was wondering if callback delegates, called in response to a event, are executed in their own thread. I was suspecting the OS might spawn a new thread and have the delegate execute in...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
1
by: MuZZy | last post by:
Hi, Is there a way to remove all event handlers for a control's event? Say, i have a button and i want to remove all button.Click events for it - i don't know how many of them was hooked to the...
0
by: Adam J. Schaff | last post by:
Hello. I have a custom collection that implements IBindingList (allownew and allowremove are both true). I have bound it to a datagrid. I have add and remove buttons on the screen. I want to...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.