开发者

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 开发者_StackOverflow中文版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.


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜