Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Can I do an event handler in a collection class to capture an event raised by one of its objects

Can I do an event handler in a collection class to capture an event raised by one of its objects

Scheduled Pinned Locked Moved Visual Basic
helptutorialannouncement
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TJO1
    wrote on last edited by
    #1

    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

    R O 2 Replies Last reply
    0
    • T TJO1

      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

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • T TJO1

        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

        O Offline
        O Offline
        OICU812
        wrote on last edited by
        #3

        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 Get

        End 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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups