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

Consuming event of a class in collection

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 22 '05 #1
2 1214
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 22 '05 #2
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 22 '05 #3

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

Similar topics

2
by: rawCoder | last post by:
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...
13
by: DraguVaso | last post by:
Hi, I have a Generic List, for instance: Public MyPersonnesDeContact As New System.Collections.Generic.List(Of clsPersonne) My clsPersonne raises some events, and I want to be able to handle...
2
by: Craig Douthitt via DotNetMonster.com | last post by:
I am trying to capture an buttonclick on a usercontrol in the webform the usercontrol resides in. After researching this issue, I've come to believe that the best way of handling this is by raising...
27
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the...
6
by: Charles Law | last post by:
I have a class, which implements an interface. Let's say, that the interface looks something like Public Interface IEventSinks Sub ValueChanged(sender As Object, e As ValueChangedEventArgs) Sub...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
6
by: kbs | last post by:
Hi, I'm looking for some good examples that illustrate how to code a web service that exposes a custom collection so that the properties of the collection are accessible on the client without...
3
by: Jeremy Chapman | last post by:
I've writtin a very simple web service in axis which returns an array of classes. I consume it in a .net app. When receiving the response, my .net app generates an error "Cannot assign object...
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...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.