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!
The Implements keyword is new to VB.NET. Its addition means that the implementation of a property, function, procedure, or event does not have to use the name defined by the interface.
Classes that implement an interface must implement all its properties, methods, and events. The following example defines two interfaces. The second interface, Interface2 , inherits Interface1 and defines an additional property and method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With