title: Using WithEvents in vb6 with .Net (VB) dll
body: <p>I have researched, and read just about everything I could find on this. It's very likely that I have seen the solution to my problem, but fail to recognize it. It seems the forest is too thick for me.</p>
  
  <p>I have written a dll in vb.net - VS2010. It works perfectly when used in a vb.net project. I have enabled the COM, etc. to allow it to work with VB6.</p>
  
  <p>Up until I need the events, it works just as planned in VB6. But now trying to get the WithEvents part to work is where I'm completely lost!</p>
  
  <p>My VB.Net dll raises an event once a time compare function becomes true (or equal). It also returns the actual time of the event. </p>
  
  <p>In my VB6 project, using the WithEvents function requires the declaration be within a class module. I can't get my head around how this would/should work.
  As I stated earlier, many post I have read point to the fact it must be in a class module, but none address exactly how to accomplish this in a clean straight forward method. Well, at least what seems straight-forward to me, anyway.</p>
  
  <p>The following code is how I make use of the dll prior to adding an event. All properties, and actions, etc. work correctly. This is located in a .bas module.</p>
  
  <pre><code>Public PT As New PrecisionTimer.PrecisionTimer
  </code></pre>
  
  <p>This is example of how it might be used on a form:</p>
  
  <pre><code>Private Sub Command3_Click()
  Dim aa As Integer
  
      aa = PT.SetTimerEvent(Val(Text6.Text))
      Select Case aa
          Case -1
              OkToInitiate = True
          Case False
              OkToInitiate = False
      End Select
  
  End Sub
  </code></pre>
  
  <p>But now I need to react to the event within the dll.
  Here is the line in the VB.Net dll that raises the event:</p>
  
  <pre><code>RaiseEvent OTE(ActualTriggeredTime.ToString())
  </code></pre>
  
  <p>So, can someone assist me, I'm really having a lot of trouble figuring out how to accomplish this.</p>
  
  <p>Thanks!</p>
  
  <p>More Info:<br>
  @TnTinMn, To a point you are right, CM has never been my strong suit. Here is the code in MyClass:</p>
  
  <pre><code>Option Explicit
  
  Public WithEvents PTM As PrecisionTimer.PrecisionTimer
  
  Private Sub PTM_OTE(ByVal ActualTriggeredTime As Variant)
  Dim aa As Integer
     aa = 1
  End Sub
  </code></pre>
  
  <p>Now the PTM_OTE sub can be deleted, and a new one will automatically appear, without the aa = 1 code of course.</p>
  
  <p>In my Module1.bas:</p>
  
  <pre><code>Option Explicit
  Public PT As New MyClass.PTM
  Public OkToInitiate As Boolean
  </code></pre>
  
  <p>Do you see an error in my code?</p>
  
  <p>In the Object Browser, I do see the OTE Event. At this point, I'm thinking I should be able to see PT in the Code Editor drop down in the upper left hand corner, but I don't?!?!</p>
  
  <p>More info:<br>
  I added an additional function to the event routine in the VB.Net dll - a TriggeredFlag.</p>
  
  <pre><code>Public Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
     'Other code including basic error handling       
     RaiseEvent OTE(ActualTriggeredTime.ToString())
     TriggeredFlag = True
  End Sub
  </code></pre>
  
  <p>Now when I use the dll in my vb6 project, I can monitor the TriggeredFlag. It does change as expected. Yet the Event doesn't seem to be received. I beleive the problem is in the VB6 code - the WithEvents statement (maybe) in the class module. Here is my entire Class Module:  </p>
  
  <pre><code>Option Explicit
  
  Public WithEvents PTM As PrecisionTimer.PrecisionTimer
  
  Public Sub Class_Initialize()
      Set PTM = New PrecisionTimer.PrecisionTimer
  End Sub
  
  Private Sub PTM_OTE(ByVal ActualTriggeredTime As String)
  Dim aa As Integer
      aa = 1
  End Sub
  </code></pre>
  
  <p>Final additional info:  </p>
  
  <p>Thanks to all that assisted in resolving this!
  Here is what my Module1.Bas ended up as:  </p>
  
  <pre><code>Option Explicit
  
  Public PT As New PrecisionTimer.PrecisionTimer
  Public DT As New DTParams
  
  Public OkToInitiate As Boolean
  </code></pre>
  
  <p>In the General section of the form:</p>
  
  <pre><code>Option Explicit
  
  Public WithEvents TimerEvents As PrecisionTimer.PrecisionTimer
  </code></pre>
  
  <p>The Form_load routine includes:</p>
  
  <pre><code>Set TimerEvents = PT
  </code></pre>
  
  <p>This is the TimerEvents_OTE routine:</p>
  
  <pre><code>Private Sub TimerEvents_OTE(ByVal ActualTriggeredTime As String)
  'Fires when the "Timer Done" event fires
  
      Call SetTimerText(ActualTriggeredTime)
  
  End Sub
  </code></pre>
  
  <p>And finally, no Class Module was used.</p>
  
  <p>Thanks again all!</p>