473,386 Members | 1,873 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,386 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 17 '05 #1
2 1076
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 17 '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 17 '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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.