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