473,769 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Events raised by collections

Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollecti on) inherited from
Collections.Has htable. Each of the Device objects can raise an event and I
need the managing class (Devices) to be able to catch these events.

Public Class Device

Public Event StatusChange()

.... rest of class

End Class

Public Class DeviceCollectio n

Inherits Hashtable

... rest of class

End Class

Public Class Devices

private mDevices as DeviceCollectio n

.... class

End Class

Can someone set me on the right path to a solution please,

Sid.
Aug 3 '07 #1
4 3994
I believe this is how you would do it for a collection:

For Each c as Device in mDevices
AddHandler c.MyDeviceEvent , AddressOf MyLocalEventHan dler
Next
"Sid Price" <si*@nowhere.co mwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollecti on) inherited from
Collections.Has htable. Each of the Device objects can raise an event and I
need the managing class (Devices) to be able to catch these events.

Public Class Device

Public Event StatusChange()

... rest of class

End Class

Public Class DeviceCollectio n

Inherits Hashtable

.. rest of class

End Class

Public Class Devices

private mDevices as DeviceCollectio n

... class

End Class

Can someone set me on the right path to a solution please,

Sid.


Aug 4 '07 #2
"Terry Olsen" <to******@hotma il.comwrote in
news:#F******** ******@TK2MSFTN GP06.phx.gbl:
I believe this is how you would do it for a collection:

For Each c as Device in mDevices
AddHandler c.MyDeviceEvent , AddressOf MyLocalEventHan dler
Next
"Sid Price" <si*@nowhere.co mwrote in message
news:%2******** *******@TK2MSFT NGP06.phx.gbl.. .
>Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollecti on) inherited from
Collections.Ha shtable. Each of the Device objects can raise an event
and I need the managing class (Devices) to be able to catch these
events.

Public Class Device

Public Event StatusChange()

... rest of class

End Class
You may also want to change the StatusChange event to contain a
reference to the sending element:

Public Event StatusChanged(B yval sender as Device)

That'll allow you to access the sending Device easily in the global
event handler.

If you do not want to pass references of the devices all over the place,
you can just pass parameters back to the event handler.
Aug 4 '07 #3
On Aug 3, 5:48 pm, "Sid Price" <s...@nowhere.c omwrote:
Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollecti on) inherited from
Collections.Has htable. Each of the Device objects can raise an event and I
need the managing class (Devices) to be able to catch these events.

Public Class Device

Public Event StatusChange()

... rest of class

End Class

Public Class DeviceCollectio n

Inherits Hashtable

.. rest of class

End Class

Public Class Devices

private mDevices as DeviceCollectio n

... class

End Class

Can someone set me on the right path to a solution please,

Sid.
Option Strict On
Option Explicit On

Imports System
Imports System.Collecti ons.Generic

Module Module1

Sub Main()
Dim manager As New DeviceManager
manager.AddDevi ce(New Device("First") )
manager.AddDevi ce(New Device("Second" ))
manager.AddDevi ce(New Device("Third") )
manager.AddDevi ce(New Device("Fourth" ))
Console.ReadLin e()
End Sub

Friend Class DeviceManager
Private _deviceCollecti on As DeviceCollectio n

Public Sub New()
_deviceCollecti on = New DeviceCollectio n()
AddHandler _deviceCollecti on.StatusChange d, AddressOf
StatusChangedHa ndler
End Sub

Public Sub AddDevice(ByVal d As Device)
_deviceCollecti on.Add(d)
End Sub

Private Sub StatusChangedHa ndler(ByVal sender As Object, ByVal
e As EventArgs)
Console.WriteLi ne(CType(sender , Device).Name)
End Sub
End Class

Friend Class DeviceCollectio n
Implements ICollection(Of Device)
Private _devices As New List(Of Device)

Public Event StatusChanged As EventHandler

Public Sub Add(ByVal item As Device) Implements
System.Collecti ons.Generic.ICo llection(Of Device).Add
AddHandler item.StatusChan ged, AddressOf
StatusChangedHa ndler
_devices.Add(it em)
End Sub

Public Sub Clear() Implements
System.Collecti ons.Generic.ICo llection(Of Device).Clear
For Each item As Device In _devices
RemoveHandler item.StatusChan ged, AddressOf
StatusChangedHa ndler
Next
_devices.Clear( )
End Sub

Public Function Contains(ByVal item As Device) As Boolean
Implements System.Collecti ons.Generic.ICo llection(Of Device).Contain s
Return _devices.Contai ns(item)
End Function

Public Sub CopyTo(ByVal array() As Device, ByVal arrayIndex As
Integer) Implements System.Collecti ons.Generic.ICo llection(Of
Device).CopyTo
_devices.CopyTo (array, arrayIndex)
End Sub

Public ReadOnly Property Count() As Integer Implements
System.Collecti ons.Generic.ICo llection(Of Device).Count
Get
Return _devices.Count
End Get
End Property

Public ReadOnly Property IsReadOnly() As Boolean Implements
System.Collecti ons.Generic.ICo llection(Of Device).IsReadO nly
Get
Return False
End Get
End Property

Public Function Remove(ByVal item As Device) As Boolean
Implements System.Collecti ons.Generic.ICo llection(Of Device).Remove
If _devices.Contai ns(item) Then
RemoveHandler item.StatusChan ged, AddressOf
StatusChangedHa ndler
_devices.Remove (item)
End If
End Function

Public Function GetEnumerator() As
System.Collecti ons.Generic.IEn umerator(Of Device) Implements
System.Collecti ons.Generic.IEn umerable(Of Device).GetEnum erator
Return _devices.GetEnu merator()
End Function

Public Function GetEnumerator1( ) As
System.Collecti ons.IEnumerator Implements
System.Collecti ons.IEnumerable .GetEnumerator
Return _devices.GetEnu merator()
End Function

Private Sub StatusChangedHa ndler(ByVal sender As Object, ByVal
e As EventArgs)
RaiseEvent StatusChanged(s ender, e)
End Sub
End Class

Friend Class Device
Private _name As String
Public Event StatusChanged As EventHandler
Private _timer As System.Threadin g.Timer

Public Sub New(ByVal name As String)
_name = name
_timer = New System.Threadin g.Timer(Address Of TickHandler,
Nothing, 1000, 1000)
End Sub

Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property

Private Sub TickHandler(ByV al state As Object)
RaiseEvent StatusChanged(M e, New System.EventArg s())
End Sub
End Class
End Module

HTH

--
Tom Shelton

Aug 4 '07 #4
Tom,

Thank you so much for such a complete sample, it used several techniques I
am not familiar with so it may take a while for me to study it and transfer
the approach to my application.

Again, thank you,
Sid.
Aug 6 '07 #5

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

Similar topics

3
25287
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
15
2097
by: Rhy Mednick | last post by:
I have a class (let's call it ClassA) that I've written which has events. In another class (let's call it ClassB) I create a collection of ClassA objects. In a third class (ClassC) I create a reference to some of the ClassA objects created in ClassB. In ClassC I hook into the ClassA events with a foreach loop so that I hook each object. The code is something like this: class ClassC { void SomeMethod() { foreach (ClassA item in...
16
2900
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
1
3509
by: Apu Nahasapeemapetilon | last post by:
Hello and thank you in advance for your help. Can anyone think of a reason why this code would work properly on one PC, but not another? I've got a System.Windows.Forms.UserControl that products events which I want to consume (sink) within Internet Explorer. I'm following the instructions at: ms-http://support.microsoft.com/default.aspx?kbid=313891.
5
2778
by: Daniel | last post by:
Hey guys When you hook an event (c# 2.0 syntax): myEvent += MyMethodToFire; You need to also unsubscribe it to avoid a resource leak so that the object it is in gets garbage collected like so : myEvent -= MyMethodToFire; That's all fine, but when you use visual studio to create events for objects it never creates an unsubscribing reference, so is it puting in resource leaks? Or is this being cleared somewhere that i am not seeing?
1
4349
by: Dave A | last post by:
I have a problem that I have boiled down to a very simple example. I have a user control that displays a some data from a business object. On one screen I have a collection of these business objects and wish to display the user control multiple times. On this web page I simply bind the repeater to the data source and in the ItemDataBound event dynamically load the user control via "LoadControl()". The user control contains an auto post...
1
2068
by: Ryan | last post by:
I am trying to log events to SQL Server instead of the computers event log, but, although I get no errors, I have no luck. The webevents_events table is empty. I have a custom event that I am raising (passwordchange), and can see it beign raised successfully...just it seems to go nowhere. I have run aspnet_regsql to setup the database to handle event recording. I am fairly sure I am missing something in my web.config file. (I would...
11
3266
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When my object is destroyed, I want my object to un-register this event. If I don't then the object would never be destroyed until the object I passed in the constructor is destroyed. I have implemented a Dispose(), Dispose(bool), and ~Finalize...
7
1822
by: Scott Stark | last post by:
Hello, I've got some code working but I'm not sure that the way I implemented it is the best way. I have a custom collection class with a boolean property HasChanged that tells me if any of the data has changed. An event is raised on certain methods (Add, Remove, Insert, etc). Let me write a little code to describe what I've done and see if there's room for improvement: I want to represent a class that functions like this when...
0
9590
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.