如何将函数定义为VB.NET中ParraArray中所有函数的总和

时间:2014-08-08 10:04:15

标签: vb.net function lambda paramarray

我有一个VB.NET函数如下:

Public Function Equilibrium(ParamArray F() As Func(Of Double, Double)) As Boolean

  'I would like to define a function
  ' G(x) = sum of all F(x) 
End Function

函数的参数是一个函数F()数组,它取一个double并返回一个double。 我想在上面的函数中将函数G(x为Double)定义为Double作为所有F(x)的总和,但到目前为止我尝试过的语法错误。有人可以帮帮我吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

这似乎有效,看看你是否想到了......

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim f As New List(Of Func(Of Double, Double))
    f.Add(AddressOf fTest)
    f.Add(AddressOf fTest)
    f.Add(AddressOf fTest)
    Dim b As Boolean = Equilibrium(f.ToArray)
End Sub

Public Function fTest(value As Double) As Double
    Return Math.PI * value
End Function

Public Function Equilibrium(ParamArray F() As Func(Of Double, Double)) As Boolean
    Dim input As Double = 2.38
    Dim G As Func(Of Double, Double) =
        Function(v As Double) As Double
            Return (From fItem As Func(Of Double, Double) In F
                    Select fItem(v)).Sum
        End Function
    Dim sum As Double = G(input)
    ' ...
End Function