title: Efficient and practical way to pass a function as a parameter, when the function may have a variable number of arguments in VB.Net?
body: <p>I would like to build a class that operates on a given mathematical function. The given mathematical function could be simple or very complex, but will always have:</p>
  
  <ul>
  <li>one independent variable</li>
  <li>one result for that variable</li>
  <li><strong>an unknown and potentially large number of constants</strong></li>
  </ul>
  
  <p>For the sake of argument, let's say I just want to print the result of these functions, in reality the operations would be complex (e.g. iterating to find a root).</p>
  
  <p>I am not sure of a better way to accomplish this other than adding the variable number of arguments to a dictionary, such that I can always pass a given function as a delegate.</p>
  
  <p>Here is what I have:</p>
  
  <pre><code>Sub RunTest()
      Dim myvars As New Dictionary(Of String, Double) From {
          {"a", 3},
          {"b", 2},
          {"c", 1},
          {"d", -1}
      }
  
      Dim MyTest1, MyTest2 As MyTestClass
  
      MyTest1 = New MyTestClass(AddressOf CubicTest, 1.123, myvars)
      MyTest2 = New MyTestClass(AddressOf QuadTest, 1.123, myvars)
      MyTest1.TestPrint()
      MyTest2.TestPrint()
  End Sub
  
  Function CubicTest(ByVal xAs Double, ByVal var As Dictionary(Of String, Double)) As Double
      Return var("a") * x^ 3 + var("b") * x^ 2 + var("c") * x + var("d")
  End Function
  
  Function QuadTest(ByVal xAs Double, ByVal var As Dictionary(Of String, Double)) As Double
      Return var("b") * x^ 2 + var("c") * x + var("d")
  End Function
  
  Class MyTestClass
      Private BaseFunction As FunctionByRef
      Private TestValue As Double
      Private Variables As Dictionary(Of String, Double)
  
      Delegate Function FunctionByRef(ByVal TestValue As Double, ByVal Variables As Dictionary(Of String, Double)) As Double
  
      Sub New(ByVal BaseFunction As FunctionByRef, ByVal TestValue As Double, ByVal Variables As Dictionary(Of String, Double))
          Me.BaseFunction = BaseFunction
          Me.TestValue = TestValue
          Me.Variables = Variables
      End Sub
  
      Sub TestPrint()
          Debug.Print(Me.BaseFunction(Me.TestValue, Me.Variables).ToString())
      End Sub
  
  End Class
  </code></pre>
  
  <p>Which seems to work just fine, but also seems very clunky.</p>
  
  <p>Is there a cleaner way to accomplish this? </p>