在 VBA 中实现我自己的接口 - 错误:对象模块需要为接口“y”实现“x”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3171324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implementing my own interface in VBA - Error: Object module needs to implement 'x' for interface 'y'
提问by amr osama
How do I implement my class ClsInterface
, which has this code:
我如何实现我的类ClsInterface
,它有这个代码:
Public Function add(x As Integer, y As Integer) As Integer
End Function
in my class Class2
, which has this code:
在我的班级中Class2
,有以下代码:
Implements ClsInterface
Public Function add(x As Integer, y As Integer) As Integer
add = x + y
End Function
My test code is
我的测试代码是
Public Sub test()
Dim obj As New Class2
MsgBox obj.add(5, 2)
End Sub
This always comes up with the following error:
这总是会出现以下错误:
Microsoft Visual Basic
Compile error:Object module needs to implement 'add' for interface 'ClsInterface'
OK/Help
Microsoft Visual Basic
编译错误:对象模块需要为接口 'ClsInterface' 实现 'add'
OK/Help
but there is no help on Microsoft help (when I press on Help button).
但是没有关于 Microsoft 帮助的帮助(当我按下“帮助”按钮时)。
Any Ideas?
有任何想法吗?
回答by Alex K.
Your Class2 must look like:
您的 Class2 必须如下所示:
Implements ClsInterface
Private Function ClsInterface_add(x As Integer, y As Integer) As Integer
ClsInterface_add = x + y
End Function
Check out the drop-down boxes at the top of Class2's code window, you can see what base object you can refer to; Classor ClsInterface.
查看Class2的代码窗口顶部的下拉框,可以看到可以引用什么基础对象;类或ClsInterface。
In your test code you want:
在您想要的测试代码中:
Dim obj As New ClsInterface
If you want to call across the interface.
如果要跨接口调用。
I would also recommend naming interfaces in the form ISomeDescription
and using Dim
then Set
rather than Dim As New
.
我还建议在表单中命名接口ISomeDescription
并使用Dim
thenSet
而不是Dim As New
.