tags:

views:

31

answers:

1

how to Implements my class 'ClsInterface' which have this code

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

in my class 'Class2' which have 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

so it's always comes up with the following error:


Microsoft Visual Basic

Compile error:

Object module needs to implement 'add' for interface 'ClsInterface'

OK Help

and no help is there on Microsoft help ? (when I pressed on Help button)

any Idea?

million thanks in all cases :)

+1  A: 

Your Class2 must look like;

Implements ClsInterface

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

(Checkout 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 intefaces in the form ISomeDescription and using Set rather than As New)

Alex K.
thank you, I followed your instructions it worked without any errors but the value is always 0 !!! while it should be 7, any idea?
amr osama
In your Private Function ClsInterface_add... you need 'Return x + y' instead of 'add = x + y', otherwise the value of x+y never gets returned.
Stewbob
You need to return using; ClsInterface_add = x + y
Alex K.