BUG: Events that are raised by using the RaiseEvent statement in a constructor are not handled (832793)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SYMPTOMS

Microsoft Visual Basic 2005, Microsoft Visual Basic .NET, Microsoft Visual Studio 2005, and Microsoft Visual Studio .NET 2002 applications do not handle events that you raise by using the RaiseEvent statement in a constructor.

CAUSE

This behavior occurs because a class is in the process being created while the code in the constructor is running. Therefore, no listener can trap the event that you raise.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. Use Visual Basic 2005 or Visual Basic .NET to create a Console Application project. By default, the Module1.vb file is created.
  3. Replace the existing code in the Module1.vb file with the following code:
    Module Module1
    
       Sub Main()
          Call TestEventsInConstructor()
       End Sub
       Private WithEvents m_obj As MyEventClass
       Public Sub TestEventsInConstructor()
          m_obj = New MyEventClass() '<- This event does not raise.
          Call m_obj.MyMethod()           '<- This event raises.
       End Sub
       Private Sub m_obj_MyEvent(ByVal strSource As String) _
         Handles m_obj.MyEvent
          MsgBox("Event raised from " & strSource)
       End Sub
    
       Friend Class MyEventClass
          Public Event MyEvent(ByVal strSource As String)
          Public Sub New()
             RaiseEvent MyEvent("MyEventClass.New")
          End Sub
          Public Sub MyMethod()
             RaiseEvent MyEvent("MyEventClass.InternalMethod")
          End Sub
       End Class
    
    End Module
  4. On the Debug menu, click Start to build and to run the application. You receive a message box that contains the following text:
    Event raised from MyEventClass.InternalMethod

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

319823 How to handle events in Visual Basic .NET



For additional information, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbvs2002sp1sweep kbpending kbEvent kbbug KB832793 kbAudDeveloper