The "Right" way to capture events from class arrays?

Posted on 2006-05-14
Last Modified: 2010-04-23
I want to handle an array of objects, and events originating from each of those objects.  Below is what I’ve done; it seems to do what I want, the HandleSignal event will handle an event from either of the classes (e.g., if I call the Go function), and it will have access to all of that object’s members so that it can take the appropriate action based on the source and the state (i.e., member values) of the source.

However, as you can see I have achieved this by including a self-reference in the event declaration (i.e., passing “Me” back as a parameter).  This works but seems a little strange/inelegant to me, is this the *Right* way to do this?

Just wondering if I’m missing something…


Public Class myParentClass

    Dim tcs(20) As myChildClass

    Public Sub New()

        tcs(0) = New myChildClass("First")
        tcs(1) = New myChildClass("Second")

        AddHandler tcs(0).Signal, AddressOf Me.HandleSignal
        AddHandler tcs(1).Signal, AddressOf Me.HandleSignal

    End Sub

    Public Sub HandleSignal(ByRef tc As myChildClass, ByVal z As Integer)

        MsgBox("rx signal from " & tc.Name & ": " & z)

    End Sub

    Public Sub Go()

        Call tcs(1).SendSignal()

    End Sub

End Class

Public Class myChildClass

    Public Event Signal(ByRef tc As myChildClass, ByVal z As Integer)

    Public Name As String

    Public Sub New(ByVal nm As String)

        Name = nm

    End Sub

    Public Sub SendSignal()

        RaiseEvent Signal(Me, Rnd() * 10)

    End Sub

End Class

Question by:riceman0
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Why is that strange to you?...

    .Net does the same thing.  The events in .Net have the "sender" parameter:

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        End Sub

    So when Button1 raises its Click() event, it sends "Me", which becomes the "sender" parameter...just as you are doing.

    Author Comment


    So I lucked on to the "right" way... how unusual for me.  

    BTW, so that the purpose of the "sender" parameter?  How do you get anything useful (i.e., button-specific, like a caption) out of a System.Object?

    LVL 85

    Accepted Solution

    You cast it...after checking it's type to be sure it's actually what you expected:

        If TypeOf sender Is Button Then
            Dim btn As Button = CType(sender, Button)

            ' do something "btn"...
            btn.Text = "Hello World!"

            ' or often you have one sub setup to handle many controls...
            Select Case btn.Text
                Case "1"

                Case "2"

            End Select

        End If

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now