Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 Implements & events

Tags:

oop

events

vb6

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!

like image 286
Adam Prescott Avatar asked Jul 31 '09 16:07

Adam Prescott


People also ask

What is implements in VB net?

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.

How many types of interfaces does Visual Basic provide?

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.


1 Answers

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.

like image 186
wqw Avatar answered Sep 18 '22 05:09

wqw