Brian Hooper
Brian Hooper

Reputation: 22044

Can't implement a class in VB6

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.

Upvotes: 5

Views: 4554

Answers (4)

BobRodes
BobRodes

Reputation: 6165

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.

Upvotes: 5

onedaywhen
onedaywhen

Reputation: 57023

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

Upvotes: 14

BobRodes
BobRodes

Reputation: 6165

For a good overview of this subject, see http://msdn.microsoft.com/en-us/library/aa260635(v=vs.60).aspx#ifacebased_vbifaces .

Upvotes: 1

Justin Niessner
Justin Niessner

Reputation: 245419

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).

Upvotes: 2

Related Questions