Consuming event of a class in collection

R

rawCoder

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
 
S

Stoitcho Goutsev \(100\) [C# MVP]

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]
 
J

Jay B. Harlow [MVP - Outlook]

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

| 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
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top