Microsoft KB Archive/832793

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

Article ID: 832793

Article Last Modified on 11/27/2007

-

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
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





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 &quot;Applies to&quot; section.



Steps to reproduce the behavior
 Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Use Visual Basic 2005 or Visual Basic .NET to create a Console Application project. By default, the Module1.vb file is created.  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(&quot;Event raised from &quot; & strSource) End Sub

Friend Class MyEventClass Public Event MyEvent(ByVal strSource As String) Public Sub New RaiseEvent MyEvent(&quot;MyEventClass.New&quot;) End Sub Public Sub MyMethod RaiseEvent MyEvent(&quot;MyEventClass.InternalMethod&quot;) End Sub End Class

End Module  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



