0

Why event handler dose not respond to the event when the object created
when I tried to raise a private event at the same way the private event raised and handled as expected

Public Class Form1
    Dim WithEvents nClass As Class1
    Private Sub nClass_Created(ByVal sender As Object, ByVal e As System.EventArgs) Handles nClass.Created
        MessageBox.Show("Created !")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        nClass = New Class1
    End Sub
End Class

Public Class Class1
    Event Created(ByVal sender As Object, ByVal e As EventArgs)
    Sub New()
        'some codes

        'when finish
        RaiseEvent Created(Me, New EventArgs)
    End Sub
End Class
Mo Khalefa
  • 536
  • 1
  • 5
  • 7
  • 1
    Because you subscribed to the different instance of that class. `nClass = New Class1` will create new instance of the `Class1` type, but you subscribed to the event of another instance. – Fabio May 31 '20 at 07:01
  • 1
    What is the actual reason of the event raising? If you explain the reason maybe you will find a correct way to solve it. – Fabio May 31 '20 at 07:07
  • 1
    You need to have a reference to an object in order to handle events of that object so handling an event of an object that is yet to be created makes no sense at all. Please explain what you're trying to achieve, rather than how you're trying to achieve it. Whatever it is, it won't involve raising an event in a constructor. With regards to your code specifically, you already know when the object is created because you're creating it. Why would need the object to tell that you just did what you did? – jmcilhinney May 31 '20 at 08:08
  • @Fabio, the problem was not that the subscription was to a different instance. The issue was that there was no instance at all until the field was set, which was after the constructor had completed and therefore after the event was raised. What you suggested would be the case on the second and later click of the button but not on the first. – jmcilhinney May 31 '20 at 09:57
  • This procedure collects some data and statistics then pass them as a startup point for the rest of application parts ,and no need to be preserved in memory or recall them again,usually i pass them using ` Sub New(ByRef ` , but when i try to pass them using events i faced this problem , so why raised events from ` Sub New ` can be handled inside its class and unfortunately can not be handled from outside ! – Mo Khalefa Jun 01 '20 at 02:13
  • @MoKhalefa - " so why raised events from ` Sub New ` can be handled inside its class and unfortunately can not be handled from outside" doesn't make sense to me. What are you saying? – Enigmativity Jun 01 '20 at 02:25
  • @Enigmativity (sorry i use google translation)so add ** Private Sub anything() Handles Me.Created MessageBox.Show("anything") End Sub** inside **Class1** to see what i mean – Mo Khalefa Jun 01 '20 at 02:53
  • @MoKhalefa - Think about it in the order in which things are created and assigned - especially since fields are assigned before constructors are run and the event is a field. – Enigmativity Jun 01 '20 at 03:16

1 Answers1

2

This concept is broken, logically. In order to raise an event, there has to be an event handler attached to the event. In order to attach a handler, the object has to exist. In order to exist the constructor must complete. You'll never be able to have a constructor raise an event because the delegate list will be empty when the constructor is running (no handlers will have been added)

This would be easier to see in C#, this is how we attach events to things:

var thing = new Thing();
thing.Event += new EventHandler(NameOfSomeMethod);

VB has a similar construct:

Dim thing as New Thing()
AddHandler(thing.Event, AddressOf(NameOfSomeMethod))

As you can see, the thing has to be constructed first. The constructor code could certainly invoke the event, but there won't be any handlers attached so nothing will happen

If you want to get notified every time an object is created use a Factory pattern; the class that constructs your class can raise the event that the class has been created

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I don't think the OP wants to be notified about every created instance though, they seem to want to know when a new instance is assigned to `nClass`. Which can be done by making `nClass` `Private` and providing a `Property Let` to change it which can raise the event. (It cannot be directly turned into a property because it's `WithEvents`.) – GSerg May 31 '20 at 21:01
  • I can't be bothered; just thought you could add that to the end of yours which is very correct otherwise. – GSerg May 31 '20 at 21:08