By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

Events raised by collections

P: n/a
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.
Aug 3 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Sid Price" <si*@nowhere.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
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.


Aug 4 '07 #2

P: n/a
"Terry Olsen" <to******@hotmail.comwrote in
news:#F**************@TK2MSFTNGP06.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 MyLocalEventHandler
Next
"Sid Price" <si*@nowhere.comwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>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
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.
Aug 4 '07 #3

P: n/a
On Aug 3, 5:48 pm, "Sid Price" <s...@nowhere.comwrote:
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

--
Tom Shelton

Aug 4 '07 #4

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.