Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何将函数传递给函数?函子/函数对象在VB2010中可用吗?_Vb.net_Functor - Fatal编程技术网

Vb.net 如何将函数传递给函数?函子/函数对象在VB2010中可用吗?

Vb.net 如何将函数传递给函数?函子/函数对象在VB2010中可用吗?,vb.net,functor,Vb.net,Functor,我想做一个数值积分方法,用解析函数,在一个特定的区间内积分。对于数值积分程序,我想使用中的一些程序。问题是这些都是用C++编程的,他们使用函子将函数传递给积分方法。如何在VB 2010中执行此操作 我想初始化函数(即,为函数y(x)=a*x+b设置a=1,b=0),然后将函数传递给积分方法。然后,当集成方法调用函数时,它只调用带有一个参数的函数(即x,因为已经设置了a,b) 在VB2010中执行此操作的最佳方法是什么? 我想做一个通用的积分方法,我可以通过任何单值函数和积分极限 我刚刚开始使用V

我想做一个数值积分方法,用解析函数,在一个特定的区间内积分。对于数值积分程序,我想使用中的一些程序。问题是这些都是用C++编程的,他们使用函子将函数传递给积分方法。如何在VB 2010中执行此操作

我想初始化函数(即,为函数y(x)=a*x+b设置a=1,b=0),然后将函数传递给积分方法。然后,当集成方法调用函数时,它只调用带有一个参数的函数(即x,因为已经设置了a,b)

在VB2010中执行此操作的最佳方法是什么? 我想做一个通用的积分方法,我可以通过任何单值函数和积分极限

我刚刚开始使用VB,从目前为止我发现的情况来看,您拥有的工具似乎非常有用 -给我们一个函数的委托 -为函数使用lambda表达式的步骤 -发送一个指针/地址 -创建函数类/结构并将其提交给函数

至于现在,我最倾向于创建一个函数类。但我真的不知道怎么做。 F.ex。我为每个要集成的“uniqe函数”创建了不同的类,但是当我需要在集成函数调用中指定参数类型时,如何将它们传递给集成函数


这似乎是一个适用于许多数学运算的基本问题,因此我认为澄清这一点非常有用。

请查看
委托

应该使用要调用的函数的签名定义委托。函数“接受另一个函数”应具有您定义的委托类型的参数。然后可以创建委托实例,传递实际函数的
addressof
,并通过参数将委托实例传递给函数

一个又快又脏的例子

Public Class Test
    Public Delegate Function MyDelegate(Param1 As Integer) As Integer

    Public Function DoSomethingWithParam1(Param1 As Integer) As Integer
        Return Param1 + 1
    End Function

    Public Sub ThisFunctionTakesADelegate(f As MyDelegate)
        Dim result As Integer = f.Invoke(2)
    End Sub

    Public Sub main()
        Dim f As New MyDelegate(AddressOf DoSomethingWithParam1)

        ThisFunctionTakesADelegate(f)'pass the function "DoSomethingWithParam1" as a parameter to "ThisFunctionTakesADelegate"
    End Sub

End Class

您还应该查看
Lambdas
匿名函数
,这是定义函数调用的另一种方法,而无需使用专用的命名函数。

很抱歉代码块较长,但我想演示Lambdas和匿名函数的不同选项

'To help differentiate the type of the parameter from the return type,
'I'm being generic with the return type. This function takes any function 
'that takes a double and returns some generic type, T.
Public Function EvalEquation(Of T)(x As Double, equation As Func(Of Double, T)) As T
    Return equation(x)
End Function
首先,我们将创建一些基本函数来使用

'Solves a basic linear equation y(x) = ax + b, given a, b, and x.
Function Linear(a As Double, b As Double, x As Double) As Double
    Return a * x + b
End Function

'Return the inverse of a number (i.e. y(x) = -x)
Function Inverse(x As Double) As Double
    Return -x
End Function
还有一个函数,它接受一个函数

'To help differentiate the type of the parameter from the return type,
'I'm being generic with the return type. This function takes any function 
'that takes a double and returns some generic type, T.
Public Function EvalEquation(Of T)(x As Double, equation As Func(Of Double, T)) As T
    Return equation(x)
End Function
最后,我们将使用它

'The closest thing to a functor is probably the AddressOf keyword.
For x = 0 To 10
    Dim answer = EvalEquation(x, AddressOf Inverse)
    'Do something
Next
但是AddressOf有一些局限性。。。 EvalEquationForX需要一个只接受一个参数的函数,因此我不能简单地使用AddressOf,因为我不能传递额外的参数。然而,我可以动态地创建一个函数,它可以为我做到这一点

For x = 0 To 10
  Dim answer = EvalEquation(x, Function(x)
                                   Dim a = 1
                                   Dim b = 0
                                   Return Linear(a, b, x)
                               End Function)
  'Do something
Next
我应该注意,您可以定义一个
Func(对于T1,T2,T3,T4,…TResult)
,这样您就可以创建一个可以接受两个参数的函数并使用它

Public Function EvalEquationWithTwoParameters(Of T)(
    a As Double, b As Double, x As Double,
    equation As Func(Of Double, Double, Double, T)) As T

    Return equation(a, b, x)
End Function
然后像这样使用它:

For x = 0 To 10
    Dim answer = EvalEquationWithTwoParameters(1, 0, x, AddressOf Linear)
    'Do something
Next
希望有帮助