Events raised by collections

S

Sid Price

Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollection) inherited from
Collections.Hashtable. 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 DeviceCollection

Inherits Hashtable

... rest of class

End Class

Public Class Devices

private mDevices as DeviceCollection

.... class

End Class



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

Sid.
 
T

Terry Olsen

I believe this is how you would do it for a collection:

For Each c as Device in mDevices
AddHandler c.MyDeviceEvent, AddressOf MyLocalEventHandler
Next
 
G

Guest

I believe this is how you would do it for a collection:

For Each c as Device in mDevices
AddHandler c.MyDeviceEvent, AddressOf MyLocalEventHandler
Next

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

Public Event StatusChanged(Byval 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.
 
T

Tom Shelton

Hello,

I have a class of objects (Device) that are managed by another object
(Devices) with a collection class (DeviceCollection) inherited from
Collections.Hashtable. 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 DeviceCollection

Inherits Hashtable

.. rest of class

End Class

Public Class Devices

private mDevices as DeviceCollection

... 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.Collections.Generic

Module Module1

Sub Main()
Dim manager As New DeviceManager
manager.AddDevice(New Device("First"))
manager.AddDevice(New Device("Second"))
manager.AddDevice(New Device("Third"))
manager.AddDevice(New Device("Fourth"))
Console.ReadLine()
End Sub

Friend Class DeviceManager
Private _deviceCollection As DeviceCollection

Public Sub New()
_deviceCollection = New DeviceCollection()
AddHandler _deviceCollection.StatusChanged, AddressOf
StatusChangedHandler
End Sub

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

Private Sub StatusChangedHandler(ByVal sender As Object, ByVal
e As EventArgs)
Console.WriteLine(CType(sender, Device).Name)
End Sub
End Class

Friend Class DeviceCollection
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.Collections.Generic.ICollection(Of Device).Add
AddHandler item.StatusChanged, AddressOf
StatusChangedHandler
_devices.Add(item)
End Sub

Public Sub Clear() Implements
System.Collections.Generic.ICollection(Of Device).Clear
For Each item As Device In _devices
RemoveHandler item.StatusChanged, AddressOf
StatusChangedHandler
Next
_devices.Clear()
End Sub

Public Function Contains(ByVal item As Device) As Boolean
Implements System.Collections.Generic.ICollection(Of Device).Contains
Return _devices.Contains(item)
End Function

Public Sub CopyTo(ByVal array() As Device, ByVal arrayIndex As
Integer) Implements System.Collections.Generic.ICollection(Of
Device).CopyTo
_devices.CopyTo(array, arrayIndex)
End Sub

Public ReadOnly Property Count() As Integer Implements
System.Collections.Generic.ICollection(Of Device).Count
Get
Return _devices.Count
End Get
End Property

Public ReadOnly Property IsReadOnly() As Boolean Implements
System.Collections.Generic.ICollection(Of Device).IsReadOnly
Get
Return False
End Get
End Property

Public Function Remove(ByVal item As Device) As Boolean
Implements System.Collections.Generic.ICollection(Of Device).Remove
If _devices.Contains(item) Then
RemoveHandler item.StatusChanged, AddressOf
StatusChangedHandler
_devices.Remove(item)
End If
End Function

Public Function GetEnumerator() As
System.Collections.Generic.IEnumerator(Of Device) Implements
System.Collections.Generic.IEnumerable(Of Device).GetEnumerator
Return _devices.GetEnumerator()
End Function

Public Function GetEnumerator1() As
System.Collections.IEnumerator Implements
System.Collections.IEnumerable.GetEnumerator
Return _devices.GetEnumerator()
End Function

Private Sub StatusChangedHandler(ByVal sender As Object, ByVal
e As EventArgs)
RaiseEvent StatusChanged(sender, e)
End Sub
End Class

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

Public Sub New(ByVal name As String)
_name = name
_timer = New System.Threading.Timer(AddressOf TickHandler,
Nothing, 1000, 1000)
End Sub

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

Private Sub TickHandler(ByVal state As Object)
RaiseEvent StatusChanged(Me, New System.EventArgs())
End Sub
End Class
End Module

HTH
 
S

Sid Price

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

Sid Price

Tom,

The move of your code to my application went smoothly and it seems to
address my issue exactley. Thank you again for being so precise in your
response.

I am interested to learn more about the use of Interfaces; do you have a
pointer to a tutorial or good article about them? The online documenation is
rather terse and assumes a level of understanding that I presently do not
have.

Thanks,
Sid.
 

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