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

Remove all event listeners with dynamic attachment

Hi there

I have a question about dynamic event binding.

I have a class called Fraction in which a number of events are declared.
Some of the events are:
OnNameChanged
OnCostChanged
OnDateChanged

Objects from this class is created and removed dynamically, using a generic
collection inside a singleton class called SourceFractions.
I then have two methods in the SourceFractions class, AddFraction(Fraction
f) and RemoveFraction(Fraction f), and two events OnFractionAdded and
OnFractionRemoved of delegate type FractionEventHandler(object sender,
Fraction f).
When AddFraction is called it invokes the OnFractionAdded event and the same
goes for the other.

In another class I register for these two events, and when the
OnFractionAdded event fires I register for the Fraction events mentioned
above.

private void OnFractionAddedHandler(object sender, Fraction f)
{
f.OnNameChanged += HandleOnNameChanged;
f.OnCostChanged += HandleOnCostChanged;
f.OnDateChanged += HandleOnDateChanged;

.... do stuff ...
}

When the OnFractionRemoved event fires i then unregister with the Fraction
events like above just with -= instead.

This procedure works fine, but seems a bit tedious... I know I have to
dynamically register for the events, but the unregistration is a pain...

Now, I know that there is a delegate method called RemoveAll(Delegate
source, Delegate value), but would this be able to do the trick of
unregistering all the listeners for a particular instance of a class? And
how does it work..?

I guess I could use it in the RemoveFraction method like:

public void RemoveFraction(Fraction f){
this.source_fractions.Remove(f); // The generic class
if(OnFractionRemoved!=null)
OnFractionRemoved(this, f);
FractionEventHandler.RemoveAll(something goes in here... but what?);
}
I really hope some of you could help me in the right direction...

Thanks in advance
Kenneth
Mar 10 '06 #1
1 6041
Kenneth,

Take a look at this thread. I remember reading it a few days ago and I
think it might address your concern.

http://groups.google.com/group/micro...b376bfaf?hl=en

Hope this helps ~ Justin

Mar 10 '06 #2

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

Similar topics

6
by: Amir Hardon | last post by:
I am dynamically adding rows to a table, and each row have a button which removes it. I have successfully implemented this for mozilla but I'm having troubles with IE, here is how I did it: ...
2
by: kj | last post by:
How does one trigger an event programmatically? I'm interested in how to do this in both the "Level 0" event model as well as in the DOM Level 2 event model. Thanks! kj -- NOTE: In my...
0
by: Tom Wingert | last post by:
Hi, I am having a problem with getting My.Log.WriteException to write to the Event Log in ASP.Net 2.0 with a web service. When an error occurs, My.Log.WriteException doesn't log anything to the...
0
by: Eniac | last post by:
Hello, I've started using Enterprise Library 2.0 recently and I've encountered a problem that seems to be ... well... undocumented :) Basically, when I set a Trace Listener (formatted event...
2
by: meyousikmann | last post by:
This will be difficult to explain so bear with me. If anyone is familiar with Tibco Rendezvous and/or Microsoft Messaging, this may make more sense. I've created a hierarchy of objects that...
6
by: bgold12 | last post by:
Hey, I just want to make sure that when I remove an element I don't have to worry about the events listeners I added previously to the element. For example: // get the element by its id elem =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...

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.