Can I do an event handler in a collection class to capture an event raised by one of its objects
-
Could someone please lend some assistance to an issue which I expect is simple but eludes me at the moment. The following description is a simplification of what I am trying to do. I have a collection called PartsCollection which has an Area property which is the sum of the area properties of each object in the collection. Each object is called PartObject and has Width, Height and Area properties. When width or height is changed the area is recalculated. I wish to have the PartsCollection.Area property update when either PartObject.Width or PartObject.Height is changed for any object within the collection. I was thinking of PartObject raising a Changed event and having an event handler for this event in the PartsCollection class. I can't figure out how to wire the handler to all of the objects in the collection. I trust this description is not too cryptic. Cheers, in advance Tim
-
Could someone please lend some assistance to an issue which I expect is simple but eludes me at the moment. The following description is a simplification of what I am trying to do. I have a collection called PartsCollection which has an Area property which is the sum of the area properties of each object in the collection. Each object is called PartObject and has Width, Height and Area properties. When width or height is changed the area is recalculated. I wish to have the PartsCollection.Area property update when either PartObject.Width or PartObject.Height is changed for any object within the collection. I was thinking of PartObject raising a Changed event and having an event handler for this event in the PartsCollection class. I can't figure out how to wire the handler to all of the objects in the collection. I trust this description is not too cryptic. Cheers, in advance Tim
Your are looking for the AddHandler and RemoveHandler keywords. They are used to bind and unbind eventhandlers dynamically: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmAddHandlerStatement.asp With it you can attach events when an item gets added to your collection. But dont forget to remove your eventhandlers when an item is removed (eg. with Remove, RemovAt, Clear...)
-
Could someone please lend some assistance to an issue which I expect is simple but eludes me at the moment. The following description is a simplification of what I am trying to do. I have a collection called PartsCollection which has an Area property which is the sum of the area properties of each object in the collection. Each object is called PartObject and has Width, Height and Area properties. When width or height is changed the area is recalculated. I wish to have the PartsCollection.Area property update when either PartObject.Width or PartObject.Height is changed for any object within the collection. I was thinking of PartObject raising a Changed event and having an event handler for this event in the PartsCollection class. I can't figure out how to wire the handler to all of the objects in the collection. I trust this description is not too cryptic. Cheers, in advance Tim
I've been working on a similar problem and I handled it with Delegates. I made an example using your class descriptions to test my knowledge of delegates. I'm no expert here but this will do what I think you are looking for.
'The delegate that handles the communications between classes.
Public Delegate Sub AreaChangedHandler(ByVal sender As PartObject)Public Class PartsCollection
'simplified collection
'not intended to demonstrate a true collection class
Dim m_collection As New ArrayList
Dim m_Area As Double
Shared Event AreaChanged As AreaChangedHandler
Public Event PartAdded(ByVal part As PartObject)
ReadOnly Property Area() As Double
Get
Dim obj As PartObject
Dim totalArea As Single
For Each obj In m_collection
totalArea += obj.Area
Next
Return totalArea
End GetEnd Property Friend Sub Add(ByVal part As PartObject) m\_collection.Add(part) RaiseEvent PartAdded(part) End Sub Shared Sub OnAreaChanged(ByVal sender As PartObject) RaiseEvent AreaChanged(sender) End Sub
End Class
Public Class PartObject
Dim m_Width As Single
Dim m_Height As Single
'Shared delegate that invokes OnAreaChanged method in PartsCollection
'without having to create an instance of PartsCollection
Private Shared delegateAreaChanged As New AreaChangedHandler(AddressOf PartsCollection.OnAreaChanged)Property Height() As Single Get Return m\_Height End Get Set(ByVal Value As Single) m\_Height = Value Call AreaChanged() End Set End Property Property Width() As Single Get Return m\_Width End Get Set(ByVal Value As Single) m\_Width = Value Call AreaChanged() End Set End Property ReadOnly Property Area() As Single Get Return m\_Height \* m\_Width End Get End Property Protected Overridable Sub AreaChanged() delegateAreaChanged.Invoke(Me) End Sub
End Class