Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing my own interface in VBA - Error: Object module needs to implement 'x' for interface 'y'

Tags:

oop

excel

vba

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

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

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

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

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

Any Ideas?

like image 914
amr osama Avatar asked Jul 03 '10 11:07

amr osama


1 Answers

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

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.

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.

like image 62
Alex K. Avatar answered Sep 22 '22 03:09

Alex K.