简体   繁体   English

VB6实现和事件

[英]VB6 Implements & events

I have an older VB6 project that I'm trying to add unit tests for. 我有一个较旧的VB6项目,我正在尝试添加单元测试。 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: ITab.cls:

Option Explicit

Public Event Click(tabNumber As Integer)

Public Sub SomeOtherFunction()

End Sub

clsRealTab.cls: clsRealTab.cls:

Option Explicit
Implements ITab

Public Event Click(tabNumber As Integer)

Public Sub ITab_SomeOtherFunction()
    'code here'
End Sub

frmMain.frm: 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 . 我实现了一个名为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. 然后我将Implements ITabEventsHandler添加到我的表单中,并将表单作为ITabEventsHandler参数传递给我的clsTab初始化程序。 Instead of raising a custom Click(...) event, I can just call mTabEventsHandler.Click(...) . 我可以只调用mTabEventsHandler.Click(...)代替自定义Click(...)事件。

Thanks for the suggestion! 谢谢你的建议!

You can't "implement" source interfaces in VB6 at all. 你根本无法在VB6中“实现”源接口。 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. 如果必须通过不同的(模拟)类“实现”这些接口,则可以考虑使用回调接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM