Question Events in nested objects

Super Dave

New member
Joined
Jul 3, 2009
Messages
4
Programming Experience
3-5
I can't seem to find a lot on how to use events in nested objects. I suspect that I should have access to the events in the nested objects through intellisense if I had done things right. I have many objects with 50 or so properties and I would like to access them with out adding handlers for each one and additional events in parent objects.

Can you direct or show me the best practice for how this should be handled?

Example:
VB.NET:
Public Class Form1

    Dim WithEvents myCombinedClass As New CombinedClass

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'AddHandler myCombinedClass.MyClass1.Test1Changed, AddressOf OnText1TextChanged  < It can see it here if I uncomment it but why not in the handles argument?
        'AddHandler myCombinedClass.MyClass2.Test2Changed, AddressOf OnText2TextChanged
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'RemoveHandler myCombinedClass.MyClass1.Test1Changed, AddressOf OnText1TextChanged
    End Sub

    Sub OnText1TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ' How to you get the handles to recognize the events in myCombinedClass.MyClass1

    End Sub

    Sub OnText2TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) ' How to you get the handles to recognize the events in myCombinedClass.MyClass2

    End Sub

    Private Sub KryptonButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KryptonButton1.Click

        myCombinedClass.MyClass1.Test1 = "Hello"
        myCombinedClass.MyClass2.Test2 = "Goodbye"

    End Sub

End Class

Public Class CombinedClass

    Private _MyClass1 As New Class1
    Private _MyClass2 As New Class2

    Sub New()

    End Sub

    Public Property MyClass1 As Class1
        Get
            Return _MyClass1
        End Get
        Set(ByVal value As Class1)
            _MyClass1 = value
        End Set
    End Property

    Public Property MyClass2 As Class2
        Get
            Return _MyClass2
        End Get
        Set(ByVal value As Class2)
            _MyClass2 = value
        End Set
    End Property


End Class

Public Class Class1

    Private _Test1 As String

    ''' <summary>
    ''' Occurs when [test1 changed].
    ''' </summary>
    Public Event Test1Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Sub New()

    End Sub

    ''' <summary>
    ''' Gets or sets the test1.
    ''' </summary>
    ''' <value>The test1.</value>
    Public Property Test1 As String

        Get
            Return _Test1
        End Get
        Set(ByVal value As String)
            _Test1 = value
            RaiseEvent Test1Changed(Me, System.EventArgs.Empty)
        End Set

    End Property

End Class

Public Class Class2

    Private _Test2 As String

    Public Event Test2Changed(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Sub New()

    End Sub

    Public Property Test2 As String

        Get
            Return _Test2
        End Get
        Set(ByVal value As String)
            _Test2 = value
            RaiseEvent Test2Changed(Me, System.EventArgs.Empty)
        End Set

    End Property


End Class
 
Last edited by a moderator:
For future reference, please use [code] tags when posting code in future.

As for the question, the code you have is fine. I just uncommented the AddHandler and RemoveHandler lines and it worked exactly as it should.

If you're asking how you can do it such that you can use a Handles clause then the key is the WithEvents keyword. You can ONLY include member variables declared WithEvents in a Handles clause. You declare 'myCombinedClass' WithEvents but that only applies to that variable. 'myCombinedClass.MyClass1' is not a member variable declared WithEvents so you can't include it in a Handles clause. To use a Handles clause you would need another member variable:
VB.NET:
Private myCombinedClass As New CombinedClass
Private WithEvents myClass1 As Class1 = myCombinedClass.Class1
Private WithEvents myClass2 As Class2 = myCombinedClass.Class2
You can now include 'myClass1' and 'myClass2' in a Handles clause. If the value of those properties of the CombinedClass object ever change, you will need to manually update your fields or the event handlers will continue listening to the old objects rather then the new.
 
Back
Top