[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

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

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

  • 2
  • 2
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
riceman0Author Commented:

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?

Mike TomlinsonMiddle School Assistant TeacherCommented:
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
riceman0Author Commented:

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now