473,324 Members | 2,193 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,324 software developers and data experts.

How to remove a given delegate from the delegate invocation list

Hi folks,
Consider the following property of a class:

public AnotherClass AnotherClassValue
{
set
{
if(this.anotherClassInstance != null)
this.anotherClassInstance.Changed -= new
EventHandler(anotherClassInstance_Changed);

this.anotherClassInstance = value;

if(this.anotherClassInstance != null)
this.anotherClassInstance.Changed += new
EventHandler(anotherClassInstance_Changed);
}
}

in which it needs to receive the Changed event of AnotherClass. If the
following line of code is called twice,

this.anotherClassInstance.Changed += new
EventHandler(anotherClassInstance_Changed);

the anotherClassInstance_Changed event handler is also get called
twice. Therefore, I've to register for the Changed event once.
However, I've got no idea how many times the AnotherClassValue
property gets called. Therefore, I've to unsubscribe to the event
(using the -= operator) first.

However, as you see, the code is not really brilliant. Is there any
other solution to the above-mentioned problem?

TIA,
Mehdi

Feb 18 '07 #1
2 1430
That looks about right / normal. The main thing I would change is a
short-circuit at the top:

if(ReferenceEquals(anotherClassInstance, value)) return; // no change

Marc

Feb 18 '07 #2
On Feb 18, 2:27 am, "mehdi" <mehdi.mous...@gmail.comwrote:
Hi folks,
Consider the following property of a class:

public AnotherClass AnotherClassValue
{
set
{
if(this.anotherClassInstance != null)
this.anotherClassInstance.Changed -= new
EventHandler(anotherClassInstance_Changed);

this.anotherClassInstance = value;

if(this.anotherClassInstance != null)
this.anotherClassInstance.Changed += new
EventHandler(anotherClassInstance_Changed);
}

}

in which it needs to receive the Changed event of AnotherClass. If the
following line of code is called twice,

this.anotherClassInstance.Changed += new
EventHandler(anotherClassInstance_Changed);

the anotherClassInstance_Changed event handler is also get called
twice. Therefore, I've to register for the Changed event once.
However, I've got no idea how many times the AnotherClassValue
property gets called. Therefore, I've to unsubscribe to the event
(using the -= operator) first.

However, as you see, the code is not really brilliant. Is there any
other solution to the above-mentioned problem?
That's pretty-much the way you have to do it. If the caller changes
the property from one value to another, and you've subscribed to
events from the old value, you have to unsubscribe from those events,
then re-subscribe to the events from the new value.

Feb 18 '07 #3

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

Similar topics

1
by: Lex | last post by:
I have an app that allows for cross process eventing. I have a server that instantiates a shared object that external processes can remotely get a refernce to and register for events. The...
3
by: N8 | last post by:
I am trying to get an exception to occur and consequently found that when adding a target method to a delegates invocation list, a copy of that object is added instead of a reference to the object....
2
by: Ed A | last post by:
Hi all: My class has one event that gets fired on a timer, typically only one function consumes it on the receiving end. // Source Class public delegate void DataHandler(ArrayList aList);...
5
by: - | last post by:
Hi to All, Is there any trick for adding additional runtime info (for example a string) to a delegate instance? I would like to mimic the Java EventListener - EventListenerProxy,...
0
by: Nate | last post by:
I am creating an application that is quite complex. There will be several dozen objects, most of which have delegates defined. At runtime, there will be several hundred instances of some objects...
20
by: David Levine | last post by:
I ran into a problem this morning with event accessor methods that appears to be a bug in C# and I am wondering if this a known issue. public event SomeDelegateSignature fooEvent; public event...
5
by: needin4mation | last post by:
Learning about delegates (again, I admit), I think I finally get it, maybe. I can reference any method in any class in the same namespace as long as it has the same signature. Right? But how...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
4
by: Bob Cramer | last post by:
I don't have a copy of Reflector handy :-( so I'd appreciate if someone could confirm (or clarify) the following. In consideration of the following delegate declaration... delegate string...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.