473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double-mapped delegate

If two delegates are created that point to the exact same method, and an
event is assigned both delegates, ...

myObj.MyEvent += new EventHandler(My Handler);
myObj.MyEvent += new EventHandler(My Handler);

.... does it recognize the duplicate method and ignore the second one, or
does it execute the same method twice when raised?

Jon
Nov 15 '05 #1
4 2525
You'll delegate called twice.

Regards
Lee
"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message
news:uD******** ******@TK2MSFTN GP10.phx.gbl...
If two delegates are created that point to the exact same method, and an
event is assigned both delegates, ...

myObj.MyEvent += new EventHandler(My Handler);
myObj.MyEvent += new EventHandler(My Handler);

... does it recognize the duplicate method and ignore the second one, or
does it execute the same method twice when raised?

Jon

Nov 15 '05 #2
Jon,

Just a side note, this is very easy to test...

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message
news:uD******** ******@TK2MSFTN GP10.phx.gbl...
If two delegates are created that point to the exact same method, and an
event is assigned both delegates, ...

myObj.MyEvent += new EventHandler(My Handler);
myObj.MyEvent += new EventHandler(My Handler);

... does it recognize the duplicate method and ignore the second one, or
does it execute the same method twice when raised?

Jon

Nov 15 '05 #3
Even easier to ask. ;)

Jon

"Nicholas Paldino [.NET/C# MVP]" <ni************ **@exisconsulti ng.com> wrote
in message news:u5******** ******@TK2MSFTN GP10.phx.gbl...
Jon,

Just a side note, this is very easy to test...

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message
news:uD******** ******@TK2MSFTN GP10.phx.gbl...
If two delegates are created that point to the exact same method, and an
event is assigned both delegates, ...

myObj.MyEvent += new EventHandler(My Handler);
myObj.MyEvent += new EventHandler(My Handler);

... does it recognize the duplicate method and ignore the second one, or
does it execute the same method twice when raised?

Jon


Nov 15 '05 #4
>> You can check whether a delegate contains a given event handler by
iterating
over the return value of GetInvocationLi st and using the overridden equality
operator (==) to check. <<

This is an excellent and very relevant answer, Simon, thank you very much!

Jon
"Simon Trew" <ten.egnaro@wer ts> wrote in message
news:ux******** ******@TK2MSFTN GP11.phx.gbl...
The Remarks section of http://tinyurl.com/hsmt (MSDN Delegate.Combin e
documentation) states:

"The invocation list can contain duplicate entries; that is, entries that
refer to the same method on the same object."

The "Return value" section of http://tinyurl.com/hsne (MSDN Delegate.Remove documentation) states:

If the invocation list of value matches a contiguous set of elements in the invocation list of source, then the invocation list of value is said to
occur within the invocation list of source. If the invocation list of value occurs more than once in the invocation list of source, the last occurrence is removed.

So also, you will need to remove (using -= in C#) the same number of
duplicates as you added.

You can check whether a delegate contains a given event handler by iterating over the return value of GetInvocationLi st and using the overridden equality operator (==) to check.

e.g.

bool DelegateContain sHandler(Delega te delegate, EventHandler handler)
{
bool bFound = false;
foreach (Delegate d in MyObj.MyEvent.G etInvocationLis t())
{
bFound = d == handler;
if (bFound) break;
}
return bFound;
}

"Jon Davis" <jo*@REMOVE.ME. PLEASE.jondavis .net> wrote in message
news:uD******** ******@TK2MSFTN GP10.phx.gbl...
If two delegates are created that point to the exact same method, and an
event is assigned both delegates, ...

myObj.MyEvent += new EventHandler(My Handler);
myObj.MyEvent += new EventHandler(My Handler);

... does it recognize the duplicate method and ignore the second one, or
does it execute the same method twice when raised?

Jon


Nov 15 '05 #5

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

Similar topics

12
9895
by: Sydex | last post by:
When I compile code I get error C2664: 'Integration::qgaus' : cannot convert parameter 1 from 'double (double)' to 'double (__cdecl *)(double)' in this part : double Integration::quad2d(double (*func)(double,double)) { nfunc = func ; return qgaus(f1,x1,x2);//error there
20
17844
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
31
6655
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one variable becomes 364 and the other one becomes 365. Does anyone have any insight to what the problem is? Thanks in advance. Bjørn
10
8663
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling func. as one of the following:
10
18776
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
67
9936
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
1
8230
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^ perimeter(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ^ setSides(double,double) in Rectangle cannot be applied to (double)...
11
3722
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want to serialize doubles in human-readable decimal form and be sure I get the exact same binary values when I read them back. (Right now, I don't care about NaN, infinities etc.)
2
5598
by: dj10fld | last post by:
I am getting a (cannot convert double to double in assignment errors) here is a part of my code #include <iostream> #include <iomanip> #include <cmath> using namespace std; #define MaxSize 1000 double loancalc (double *months, double *intrat, double *princ, double calcprnc, double calcpay, double calcbal, double calcnew, double calcInt); double display (double *months, double calcprnc, double calcpay, double calcbal, double...
2
5808
by: Genro | last post by:
#include<stdio.h> #include<TX/graphics.h> #include<time.h> // I need help! struct Krug{ double _x; double _y; double _skox; double _skoy; double _granx1;
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.