代码之家  ›  专栏  ›  技术社区  ›  amr osama

在VBA中实现我自己的接口-错误:对象模块需要为接口“y”实现“x”

  •  11
  • amr osama  · 技术社区  · 14 年前

    如何实现我的课程 ClsInterface ,其代码为:

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

    Class2 ,其代码为:

    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
    

    这总是会出现以下错误:

    Microsoft Visual Basic
    编译错误:


    好的/救命

    有什么想法吗?

    1 回复  |  直到 6 年前
        1
  •  15
  •   Alex K.    6 年前

    你的班级2必须看起来像:

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

    查看Class2代码窗口顶部的下拉框,可以看到可以引用的基本对象; CLS接口 .

    Dim obj As New ClsInterface
    

    ISomeDescription 以及使用 Dim Set 而不是 Dim As New .