将名称过程传递给vb6中的另一个过程

时间:2017-08-07 23:21:07

标签: function vb6

我需要VB6这样的东西:

private sub function1 (a1 as integer, a2 as integer) as double
  'code of function1
end function

private sub function2 (b1 as integer, b2 as integer) as double
  'code of function2
end function

private sub function3 (kk as string) as string
  dim c1 as double
  dim c2, c3 as integer
  c1 = kk(c2,c3)
  ' In kk appear the name of function1 or function2
end function

这段代码错了。
将控制名称传递给另一个程序,我读到这是可能的,
但是,在vb6中是否可以使用名称功能?

由于

1 个答案:

答案 0 :(得分:0)

在不同情况下调用不同代码的标准方法是不能基于String调用方法。一般来说,做这种事情的方式,你希望能够传递到"您要使用的方法是使用VB具有的多态性,并为每个实现使用单独的类。

在您用于界面的类模块中,例如名为MyInterface

Public Function DoStuff(a1 as Integer, a2 as Integer) As Double
End Function

然后,创建两个类模块,每个模块用于接口的每个可能实现:

MyClassA

Implements MyInterface
Public Function MyInterface_DoStuff(a1 as integer, a2 as integer) As Double
    'code of function 1
End Function

然后,在MyClassB,同样的事情,但与你的其他实现:

Implements MyInterface
Public Function MyInterface_DoStuff(a1 as integer, a2 as integer) As Double
    'code of function 2
End Function

要使用它,您需要在实现MyInterface的实例中传递调用者:

Private Sub Function3 (kk as MyInterface)
  dim c1 as double
  dim c2, c3 as integer
  c1 = kk.DoStuff(c2,c3)
  ' In kk appear any instance of MyInterface that you want to use
End Sub

调用Function3的一个例子:

Dim kk as MyInterface
Set kk = New MyClassA
Function3 kk

要在MSDN库中进一步阅读: