title: Handling VB.NET events in VB6 code
body: <p>I have some VB6 code that instantiates a class which handles events that are being raised from a VB.NET component. The VB6 is pretty straightforward:</p>
  
  <pre><code>private m_eventHandler as new Collection
  
  ...
  
  public sub InitSomething()
    dim handler as EventHandler
  
    set handler = new EventHandler
    m_eventHandler.Add handler
    ...
    m_engine.Start
  
  end sub 
  </code></pre>
  
  <p>Note that the event handler object has to live beyond the scope of the init method (which is why it is being stored in a Collection). Note also that  <code>m_engine.Start</code> indicates the point in the program where the VB.NET component would start raising events.</p>
  
  <p>The actual event handler (as requested):</p>
  
  <pre><code>Private WithEvents m_SomeClass As SomeClass
  Private m_object as Object
  ...
  
  Private Sub m_SomeClass_SomeEvent(obj As Variant)
      Set obj = m_object
  End Sub
  </code></pre>
  
  <p>Note that <code>m_object</code> is initialized when an instance of <code>EventHandler</code> is created. </p>
  
  <p>The VB.NET code which raises the event is even simpler:</p>
  
  <pre><code>Public ReadOnly Property SomeProp() As Object
      Get
          Dim obj As Object
          obj = Nothing
          RaiseEvent SomeEvent(obj)
          SomeProp = obj
      End Get
  End Property
  </code></pre>
  
  <p>My problem is that when I <strong>debug</strong> the VB6 program, the first time <code>InitSomething</code> gets called, the event will <strong>not</strong> be handled (the VB6 event handler is never entered). Subsequent calls to <code>InitSomething</code> does work.</p>
  
  <p>Everything works as I would have expected when I run the program outside the debugger. At this point, I'm not even sure if this is something I should be worried about.</p>
  
  <p>It may or may not be relevant but the VB.NET was converted from a VB6 using the Visual Studio code conversion tool (and subsequently manually cleaned up).</p>