By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,640 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,640 IT Pros & Developers. It's quick & easy.

Consuming event of a class in collection

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
"rawCoder" <ra******@hotmail.com> wrote in message
news:%2******************@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

Nov 21 '05 #2

P: n/a
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

"rawCoder" <ra******@hotmail.com> wrote in message
news:%2******************@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
|
|
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.