Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PV Function and Porting VB6 to C#

I'm working on porting some classic VB6 code to C# and just stumbled across a usage of the PV function.

I feels wrong including a reference to the Microsoft.VisualBasic Assembly. Is this something that is commonly done, or should I explore further options. The next idea that pops into my mind is exploring this PV function in Reflector.

like image 468
David Avatar asked Sep 24 '09 14:09

David


3 Answers

The use of Microsoft.VisualBasic from C# and VB.NET has been discussed thoroughly under this question. The Microsoft.VisualBasic namespace is fully supported, and will be around as long as .Net is around. There's no reason to avoid it.

EDIT: It's telling that at the time of typing, the other answers for this question are an incorrect reimplementation of the function, and a one-man-band unsupported library from Code Galleries. Come on guys, it would take a real major event for Microsoft to drop the financial functions from VB.

It's a different story for Microsoft.VisualBasic.Compatibility, which is exclusively for use by the VB6 upgrade wizard, EDIT has now been marked obsolete in .Net 4 (my prediction came true), and should not be used for new development. There would be some advantages in removing references to this, but personally I'd probably try to achieve a fully working port first referencing.Net 3.5.

like image 94
MarkJ Avatar answered Oct 18 '22 00:10

MarkJ


Pretty straight forward to replicate in C#

    public static double PV(double Rate, int nPer, double Pmt, double FV, bool Type)
    {
        double ann = Math.Pow(1 + Rate, nPer);
        return -(FV + Pmt * (1 + (Type ? Rate : 0)) * ((ann - 1) / Rate)) / ann;
    }

Just a re-arrangement of the formula Microsoft provides.

like image 35
JDunkerley Avatar answered Oct 18 '22 02:10

JDunkerley


You could use this library that duplicates excel functions in f#

Library

It has PV which is present value. I have used it once or twice. Just drop it in and add the reference.

like image 30
Gratzy Avatar answered Oct 18 '22 00:10

Gratzy