Link to home
Start Free TrialLog in
Avatar of vdefabis
vdefabis

asked on

VB.NET Event not Being Handled

Hello Experts,

I am just dipping into custom events and have hit a major snag. An event raised in one class is not being handled when another class initiates the sub that contains the event. I have attached my code. All help is greatly appreciated.
Public Class ResultCollection
        Inherits CollectionBase
        Public Event ResultLoaded(ByVal CurrentRecord As Integer, ByVal TotalRecords As Integer)
        Default Public Overridable ReadOnly Property Item(ByVal index As Integer) As Result
            Get
                Return CType(Me.List(index), Result)
            End Get
        End Property

        Public Sub Add(ByVal Result As Result)
            List.Add(Result)
        End Sub
        Public Sub New()

        End Sub
        Public Sub New(ByVal CycleCountCompletionID As Integer)
            Dim CurrCount As Integer = 0
            Dim TotalRecords As Integer = 0

            Dim SQL As String = [OMITTED]
            Dim DS As DataSet = VSMS.GetDataSet(SQL, VSMS.TRIPConnStr)
            TotalRecords = DS.Tables(0).Rows.Count

            For Each DR As DataRow In DS.Tables(0).Rows
                CurrCount += 1
                Me.Add(New Result(Convert.ToInt32(DR.Item("CycleCountResultID"))))
                RaiseEvent ResultLoaded(CurrCount, TotalRecords) '<-- EVENT FIRES HERE AND IS HANDLED PROPERLY IF I ADD AN EVENT HANDLER TO THE SAME CLASS
            Next

        End Sub



    End Class

Public Class Completion
        Private WithEvents _Results As New ResultCollection

        Public Sub LoadResults()

            _Results = New ResultCollection(CycleCountCompletionID) '<--This SHOULD get the event handler to start firing events.

        End Sub
        
Public Sub _Results_ResultLoaded(ByVal CurrentRecord As Integer, ByVal TotalRecords As Integer) Handles _Results.ResultLoaded
'EVENT SHOULD BE RAISED HERE, BUT DEBUGGING SHOWS THAT IT IS NOT HITTING THIS SUB.
            Dim X As String = ""
        End Sub
End Class

Open in new window

Avatar of KingFlash
KingFlash

Public Class ResultCollection
        Inherits CollectionBase
        Public Event ResultLoaded(ByVal CurrentRecord As Integer, ByVal TotalRecords As Integer)
        Default Public Overridable ReadOnly Property Item(ByVal index As Integer) As Result
            Get
                Return CType(Me.List(index), Result)
            End Get
        End Property

        Public Sub Add(ByVal Result As Result)
            List.Add(Result)
        End Sub
        Public Sub New()

        End Sub
        Public Sub New(ByVal CycleCountCompletionID As Integer)
            Dim CurrCount As Integer = 0
            Dim TotalRecords As Integer = 0

            Dim SQL As String = [OMITTED]
            Dim DS As DataSet = VSMS.GetDataSet(SQL, VSMS.TRIPConnStr)
            TotalRecords = DS.Tables(0).Rows.Count

            For Each DR As DataRow In DS.Tables(0).Rows
                CurrCount += 1
                Me.Add(New Result(Convert.ToInt32(DR.Item("CycleCountResultID"))))
                RaiseEvent ResultLoaded(CurrCount, TotalRecords) '<-- EVENT FIRES HERE AND IS HANDLED PROPERLY IF I ADD AN EVENT HANDLER TO THE SAME CLASS
            Next

        End Sub



    End Class

Public Class Completion
       ' This is the correct statement
       Private WithEvents _Results As ResultCollection
' The Error Go Here ... No Need for the "New" Keyword ' Private WithEvents _Results As New ResultCollection
        Public Sub LoadResults()

            _Results = New ResultCollection(CycleCountCompletionID) '<--This SHOULD get the event handler to start firing events.

        End Sub
       
Public Sub _Results_ResultLoaded(ByVal CurrentRecord As Integer, ByVal TotalRecords As Integer) Handles _Results.ResultLoaded
'EVENT SHOULD BE RAISED HERE, BUT DEBUGGING SHOWS THAT IT IS NOT HITTING THIS SUB.
            Dim X As String = ""
        End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
but be careful with using shared events. behaviour may change in case you use multiple instances of the class.
Avatar of vdefabis

ASKER

Thanks to the excellent information from appari and Idle_Mind I was able to solve this problem in less than 5 minutes. Thanks a bunch!