VB6 Implements & events

eventsoopvb6

I have an older VB6 project that I'm trying to add unit tests for. I was breaking dependencies in classes by mocking objects. Everything was going great until I found some dependencies that were raising events and now I've hit a wall.

Here's a quick example of what I'm trying to do that DOESN'T WORK:

ITab.cls:

Option Explicit

Public Event Click(tabNumber As Integer)

Public Sub SomeOtherFunction()

End Sub

clsRealTab.cls:

Option Explicit
Implements ITab

Public Event Click(tabNumber As Integer)

Public Sub ITab_SomeOtherFunction()
    'code here'
End Sub

frmMain.frm:

Option Explicit

Private WithEvents mTab as ITab

Public Sub Main()
    Set mTab = New clsRealTab 'gives "Object or class does not support the set of events" error'
End Sub

Does anybody know if there's a way to make this work or another way to go about handling this situation?

I implemented a callback interface that I called ITabEventsHandler. It looks like this:

Option Explicit

Public Sub Click(intPreviousTab As Integer, objSSTab As Object)

End Sub

Then I added Implements ITabEventsHandler to my form and pass the form as an ITabEventsHandler parameter to my clsTab initializer. Instead of raising a custom Click(...) event, I can just call mTabEventsHandler.Click(...).

Thanks for the suggestion!

Best Answer

You can't "implement" source interfaces in VB6 at all. So the short answer is "no, you can't do this". You can hack it with direct typelib editing but this will become ugly very quickly.

You can consider callback interfaces in your case if you have to "implement" these by different (mock) classes.