tags:

views:

384

answers:

1

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!

+4  A: 

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.

wqw
I thought about making a callback interface and it seems like this will work.
aprescott
Seconded. The most elegant way to deal with this in VB6 is to use a callback interface instead of raw events. Typelib hacking will quickly get nasty as wqw says.
Mike Spross
+1. The VB6 manual says Implements does not support events here http://msdn.microsoft.com/en-us/library/aa262327(VS.60).aspx
MarkJ
Beware: using interfaces to replace events will likely cause a circular reference.
rpetrich