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