|
tech
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Consuming event of a class in collectionHi,
I must be missing something here. All I want is to be able to handle the events of one of my class, objects of which are in a collection. For e.g. I have a class MyClass1 which raises MyEvent1 and a collection which holds its objects. I want to be able to handle the events when any object of MyClass1 raises a MyEvent1. rawCoder Hi rawCoder,
I think you are looking for something like bubbling the events on the collection level. If you are, then I must say that the events don't bubble by them selfs. So, as far as I can see you have couples of ways to solve this. 1. To handle the events before adding the objects to the collection. This can be done also via creating custom collection that will accept a delegate in the Add method or using some other technique. 2. To declare this event as static. Here you don't have to have a reference to the object to handle the event. The drawback of this of course is that the event handler doesn't know the context of the object raised the exception. It could be in a the collection, in a separate thread actually anywhere in the same appdomain. 3. To have custom collection that hooks to the event upon accepting objects and unhooks the event when the object is removed. Then the collection will raise some more generic event giving the information about the actual even and the object that raised it. In other words implementation to some extend of the event bubbling. HTH Stoitcho Goutsev (100) [C# MVP] Show quoteHide quote "rawCoder" <rawCo***@hotmail.com> wrote in message news:%23%23owVi1aFHA.1044@TK2MSFTNGP10.phx.gbl... > Hi, > > I must be missing something here. > > All I want is to be able to handle the events of one of my class, objects > of > which are in a collection. > > For e.g. I have a class MyClass1 which raises MyEvent1 and a collection > which holds its objects. I want to be able to handle the events when any > object of MyClass1 raises a MyEvent1. > > rawCoder > > rawCoder,
As the others suggest, I normally use AddHandler in my Collection.Add routine & RemoveHandler in my Collection.Remove routine. Something like: Public Class MyClass1 Public Event MyEvent1 As EventHandler End Class Public Class MyClass1Collection Inherits CollectionBase Public Sub Add(ByVal value As MyClass1) MyBase.InnerList.Add(value) AddHandler value.MyEvent1, AddressOf MyEvent1_Handler End Sub Public Sub Remove(ByVal value As MyClass1) MyBase.InnerList.Remove(value) RemoveHandler value.MyEvent1, AddressOf MyEvent1_Handler End Sub Private Sub MyEvent1_Handler(ByVal sender As Object, ByVal e As EventArgs) End Sub End Class NOTE: I would consider overriding CollectionBase.OnInsertComplete & CollectionBase.OnRemoveComplete for the AddHandler & RemoveHandler statements... Hope this helps Jay Show quoteHide quote "rawCoder" <rawCo***@hotmail.com> wrote in message news:%23%23owVi1aFHA.1044@TK2MSFTNGP10.phx.gbl... | Hi, | | I must be missing something here. | | All I want is to be able to handle the events of one of my class, objects of | which are in a collection. | | For e.g. I have a class MyClass1 which raises MyEvent1 and a collection | which holds its objects. I want to be able to handle the events when any | object of MyClass1 raises a MyEvent1. | | rawCoder | |
Other interesting topics
Hide JIT Debug Error
which is best? How to use .NET framework to toggle the power on a USB-powered dev ItemDataBound text modification question Several Crystal Reports Doubts GPS Facing precision issues with "double" Can't open 'Samples and Quick Start Tutorials' property grid control Error creating window handle |
|||||||||||||||||||||||