Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't implement a class in VB6

Tags:

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.

like image 312
Brian Hooper Avatar asked Oct 19 '10 12:10

Brian Hooper


3 Answers

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
like image 114
onedaywhen Avatar answered Nov 09 '22 22:11

onedaywhen


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.

like image 40
BobRodes Avatar answered Nov 09 '22 21:11

BobRodes


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

like image 33
Justin Niessner Avatar answered Nov 09 '22 23:11

Justin Niessner