views:

71

answers:

3

There is a built in function from the Microsoft.VisualBasic assembly. I can use it in VB like this:

Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoanAmount) * -1

My current project is in C# and I need to use this function. Answers on the web say just add the namespace and assembly and use the same in C#- but this is not true! C# still does not recognize this formula.

So how can I use use Financial.Pmt in C# (or perhaps even porting the source code to it)? Thanks for any help.

+1  A: 

You must add a reference to the Microsoft.VisualBasic.dll assembly for your project in Visual Studio. This is not the same thing as the using Microsoft.VisualBasic; directive at the top your source file. You must do both steps.

Joel Coehoorn
+2  A: 

As already stated you can use the Microsoft.VisualBasic assembly which provides plenty of the VB6 functionality. But to be honest if you are looking more generally at financial calculations you should consider looking at Excel Financial functions for .NET.

Chris Taylor
That's a one-man-band unsupported library from Code Galleries. I'd need a strong argument to prefer using that over a fully supported core library from the .NET framework, whatever DLL it lives in. Are there many financial calcs in that library that aren't in Microsoft.VisualBasic?
MarkJ
+3  A: 

Like this:

using System;
using Microsoft.VisualBasic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double dAPR = 2;
            Int32 iNumberOfPayments = 12;
            double dLoanAmount = 10000;
            Console.WriteLine(Financial.Pmt((dAPR / 100) / 12, iNumberOfPayments, dLoanAmount, 0, DueDate.EndOfPeriod) * -1);
            Console.ReadLine();
        }
    }
}
  • Like Joel says, add a reference to the Microsoft.VisualBasic assembly.
  • Like Rup says in a comment, you have to provide the defaults to the 4th and 5th arguments.

Do use Microsoft.VisualBasic from C# when appropriate. It's a fully supported core library in .Net and it contains some useful financial functions.

MarkJ