tags:

views:

51

answers:

3

I'm trying to implement an interface in VB6. I have defined the class Cast_Speed like this...

Public Function Run_Time() As Long

End Function

and the implementation like this...

Option Explicit
Implements Cast_Speed

Public Function Cast_Speed_Run_Time() As Long
    Cast_Speed_Run_Time = 0
End Function

but attempting to compile it gives 'object module needs to implement 'Run_Time' for interface 'Cast_Speed'. Can anyone see what I am doing wrong? My subroutines seem to be quite all right, but all the functions I try have this problem.

+2  A: 

Unless I'm mistaken, Interface Implementations in VB6 needed to be private (even though the interface declares them as public).

Try changing:

Public Function Cast_Speed_Run_Time() As Long

To:

Private Function Cast_Speed_Run_Time() As Long

You can also read up on implementing interfaces in VB6 here (which seems to back me up).

Justin Niessner
@Justin Niessner - thank you for that; it has done the trick. I will study your reference.
Brian Hooper
Yes, you are mistaken. I just tested: it defaults to `Private` but manually editing the scope to `Public` works fine.
onedaywhen
@onedaywhen - Editting the scope to public would change that from an interface implementation to a regular public function. Consider the change from Public to Private fixed the problem for the OP, I'm pretty sure I'm right.
Justin Niessner
@Justin Niessner: Try for yourself the working code I just posted to my answer ;)
onedaywhen
@onedaywhen - I would, but unfortunately I haven't had a way to build VB6 code installed on my machine for years. :-P
Justin Niessner
@Justin Niessner: this is vanilla VBA so will work in Word, Excel, etc in case you have one of those. Excel is what I used myself! :)
onedaywhen
@Justin Niessner - I was a little mistaken when I said that had fixed it; it had corrected another function, but I also needed onedaywhen's help below. Thanks all the same.
Brian Hooper
@Justin @onedaywhen Justin is wrong, onedaywhen is right. I just compiled onedaywhen's code with a public function in VB6. Also there's a topic in the VB6 docs that supports onedaywhen, see my comment on that answer
MarkJ
@MarkJ - Good catch on the docs.
Justin Niessner
+5  A: 

It doesn't like the underscore character in the method name. Try using RunTime() instead.

I just tested it without the underscore and it works fine for me:

'// class Cast_Speed
Option Explicit

Public Function RunTime() As Long

End Function


'// class Class1
Option Explicit

Implements Cast_Speed

Public Function Cast_Speed_RunTime() As Long
  Cast_Speed_RunTime = 0
End Function
onedaywhen
@onedaywhen - thank you for that; it would never have occurred to me to try it.
Brian Hooper
+1 From the VB6 documentation *Creating Interfaces for Use With the Implements Statement* `Interface methods cannot have underscores in their names` http://msdn.microsoft.com/en-us/library/aa262287(VS.60).aspx
MarkJ
A: 

While you can make interface implementations public, it isn't considered good practice, any more than it is considered good practice to allow an interface to be directly instantiated as you also can do. It is simply an example of the maxim that it is possible to write extremely bad code in VB6. :)

Best practice is as follows:

  1. Interface instancing property is PublicNotCreatable.
  2. Implemented Interface Methods are scoped Private.

Thus:

Dim x as iMyInterface

Set x = new MyiMyInterfaceImplementation

x.CalliMyInterfaceMethodA

x.CalliMyInterfaceMethodY

And so on. If someone attempts to directly instantiate the interface, that should cause an error, and if someone attempts to call an implemented method directly instead of polymorphically through the interface that should return an error too.

BobRodes