在VBA中实现我自己的接口

在VBA中实现我自己的接口

本文介绍了在VBA中实现我自己的接口-错误:对象模块需要为接口"y"实现"x"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何实现我的类ClsInterface,该类具有以下代码:

How do I implement my class ClsInterface, which has this code:

Public Function add(x As Integer, y As Integer) As Integer
End Function

在我的班级Class2中,它具有以下代码:

in my class Class2, which has this code:

Implements ClsInterface

Public Function add(x As Integer, y As Integer) As Integer
add = x + y
End Function

我的测试代码是

Public Sub test()
Dim obj As New Class2
MsgBox obj.add(5, 2)
End Sub

这总是会出现以下错误:

This always comes up with the following error:

对象模块需要为接口'ClsInterface'实现'add'
确定/帮助

Object module needs to implement 'add' for interface 'ClsInterface'
OK/Help

但Microsoft帮助没有帮助(当我按帮助"按钮时).

but there is no help on Microsoft help (when I press on Help button).

有什么想法吗?

推荐答案

您的Class2必须看起来像:

Your Class2 must look like:

Implements ClsInterface

Private Function ClsInterface_add(x As Integer, y As Integer) As Integer
    ClsInterface_add = x + y
End Function

查看Class2的代码窗口顶部的下拉框,您可以看到可以引用的基础对象. Class ClsInterface .

Check out the drop-down boxes at the top of Class2's code window, you can see what base object you can refer to; Class or ClsInterface.

您想要的测试代码中:

Dim obj As New ClsInterface

如果您想通过界面拨打电话.

If you want to call across the interface.

我还建议以ISomeDescription的形式命名接口,并先使用Dim然后使用Set而不是Dim As New.

I would also recommend naming interfaces in the form ISomeDescription and using Dim then Set rather than Dim As New.

这篇关于在VBA中实现我自己的接口-错误:对象模块需要为接口"y"实现"x"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:02