Custom Event not getting Fired

madan_100

New member
Joined
Nov 14, 2007
Messages
1
Programming Experience
1-3
Please help. The ReceiveEvent is not getting fired in vb.net 1.1.
---------------------------

VB.NET:
Public Class ClsMain
    Public Event myevent()
    Public WithEvents objCls As ClsMain

    Public Sub ReceiveEvent() Handles objCls.myevent
        MsgBox("Eventfired")
    End Sub

    Shared Sub main()
        Dim o As New ClsMain
        o.MySub()
    End Sub

    Public Sub MySub()
        Try
            RaiseEvent myevent()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class
 
Last edited by a moderator:
Your class contains a reference to another possible reference of itself but this variable is null, a handler code declares that it handles the events raised by that null "instance" (it's not an instance, because it is null) but that null instance will never raise any events, not only because it is null, but because you never raise the event on that instance; you raise it on the other instance, that has no associated handler


You will find this much easier if you dont try to make a class that refers to itslef and handles its own events. THere's no point in having a class handle its own events because events are for notifying other unknown instances about something this instance has done. THis instance already knows everything about itself and doesnt need to subscribe to its own events


Make 2 classes, one with a timer, and have it expose an event and RaiseEvent it on a regualr schedule
The seconds class declares a new WithEvents instance of the first, and then makes a HANDLES declaration so that Class2 HANDLES class1Instance.Event

When Class1 raises the event, class2 is notified. Dont write such confusing code when youre undertaking a learning exercise! :)
 
objCls doesn't refer to any instance.

If you want to listen to the classes own event do this:
VB.NET:
Class something
    Public Event evt()

    Public Sub raiseit()
        RaiseEvent evt()
    End Sub

    Private Sub something_evt() [B][COLOR="SeaGreen"]Handles Me.evt[/COLOR][/B]
        MessageBox.Show("handled internal event")
    End Sub
End Class
test code (not called from the class itself)
VB.NET:
Dim x As New something
x.raiseit()
 
Come to think of it, .Net 1.1 might use "Handles Mybase.evt" by default, and not "Handles Me.evt". Just try it.

Also, there exist a common event pattern where you set up a "OnEvent" method for each event and call this internally instead of RaiseEvent, this method should then call RaiseEvent. The purpose is streamlining code and have one place to do common stuff before/after the event is raised to external subscribers, this also benefits inheritors. See .NET Framework Developer's Guide, Event Design. So the 'clean' version of the example class would be this:
VB.NET:
Class something
    Public Event Evt(ByVal sender As Object, ByVal e As EventArgs)

    Public Sub raiseit1()
        Me.OnEvt(New EventArgs)
    End Sub
    Public Sub raiseit2()
        Me.OnEvt(New EventArgs)
    End Sub

    Protected Sub OnEvt(ByVal e As EventArgs)
        MessageBox.Show("before event raised")
        RaiseEvent Evt(Me, e)
    End Sub

End Class
 
Back
Top