Delegate question

  • Thread starter Thread starter Schizoid Man
  • Start date Start date
S

Schizoid Man

Hi,

I am having trouble rewriting the following C# code in VB:

// delegate declaration
public delegate double FX(double x);

//delegate usage
FX f = delegate(double x)
{
.... code...
return answer;
};

The delegate declaration looks like:

Public Delegate Function FX(ByVal x As Double) As Double

However I'm uncertain on how to rewrite the code bit. I'd appreciate any
pointers.

Thanks,
Schiz
 
Schizoid Man said:
I am having trouble rewriting the following C# code in VB:

// delegate declaration
public delegate double FX(double x);

//delegate usage
FX f = delegate(double x)
{
... code...
return answer;
};

The delegate declaration looks like:

Public Delegate Function FX(ByVal x As Double) As Double

However I'm uncertain on how to rewrite the code bit. I'd appreciate any
pointers.

The current version of VB doesn't support anonymous functions containing
more than one special statement (Lambda expression). Thus you'll have to
define a separate function and determine a delegate to the function using
the 'AddressOf' operator.
 
Herfried K. Wagner said:
The current version of VB doesn't support anonymous functions containing
more than one special statement (Lambda expression). Thus you'll have to
define a separate function and determine a delegate to the function using
the 'AddressOf' operator.

Thank you for the answer. I now see how this can be implemented but I still
have a problem. The code in question looks like:

Sub Main()
Dim James As Bond
James = New Bond(0.1, 2, 5, 0.12, 95)
' solve for yield
Dim f As FX = (AddressOf SolveForYield)
Dim Soln As Double = NewtonMethod.DoSolve(f)
Console.WriteLine("Solution is: {0}", Soln)
Console.ReadLine()
End Sub

Private Function SolveForYield(ByVal x As Double) As Double

End Function

In the private function SolveForYield() I need to access some properties of
the James instance of the Bond class. In the NewtonMethod, my delegate is
declared as

Public Delegate Function FX(ByVal x As Double) As Double

This is because the NewtonMethod class implements a root finding solver that
only takes in a single value:

' h - Step for numerical derivative
Private Const h As Double = 0.000000000000001

' Newton method solver
Public Shared Function DoSolve(ByVal f As FX) As Double
Dim x As Double
Dim xNext As Double = 0.1 'initial guess
Do
x = xNext
xNext = x - f(x) / ((f(x + h) - f(x)) / h)
Loop While (Math.Abs(xNext - x) > h)
Return xNext
End Function


What solution would you recommend? Do I need to overload the delegate
declaration?

Thanks.
 
Herfried K. Wagner said:
The current version of VB doesn't support anonymous functions containing
more than one special statement (Lambda expression). Thus you'll have to
define a separate function and determine a delegate to the function using
the 'AddressOf' operator.

A workaround would be to declare the bond object outside the Sub Main()
method, but to instantiate within the Main() method. However, would this be
the recommended solution? It doesn't strike me as particularly elegant.
 
There is no elegant alternative to multi-statement lambda's until VB10. The
solution Herfied gave is the best one, but as you know it doesn't work for
the case of 'captured variables'.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert C++ to C#, VB, or Java
Convert Java to C#, VB, or C++
Convert VB to C#, C++, Java, or Python
Convert C# to VB, C++, Java, or Python
 
Schizoid Man said:
A workaround would be to declare the bond object outside the Sub Main()
method, but to instantiate within the Main() method. However, would this
be the recommended solution? It doesn't strike me as particularly elegant.

This is kinda what C# does under the hood anyway. For any method that
contains an anonymous function then C# will create a hidden class with the
local variables as module level variables. It's just as messy but I guess
it's hidden from the programmer.

Michael
 
Back
Top