Leveraging Base Classes in VB.NET

Introduction 

Multiple language implementation has an inherited powerful advantages. The .Net unify class architecture model allows .Net languages to leverage class libraries implemented in other class libraries. This article shows you how to leverage the power of VB.Net Financial base class library using C#.

Article

Since no language implementation can provides classes for every possible thing, C# is not the exception, when it comes to financial functions, C# nor the Math class provides you any implementation of  financial classes or functions.  If for example you need to develop a mortgage loan calculation program using C#, your basic choices are: 1) Roll your own one,  2) Use another language which implements the desired functionality, 3) Use a commercially available COM objects, or  4) Leverage the base class library of another .Net language.

In this article I'll show you a WinForm mortgage loan calculator using a home grown Pmt formula (choice #1), and I'll leverage the financial base class implemented in VB.Net (choice #4).

Running the sample code should display a form with a principal amount = 10000, an interest rate = 8, and a duration in months = 36.  By pressing the PMT button, the calculated PMT by the C# code and financial base class will show at the bottom of the window.

LVBN1-Vb.net.gif

The actual code that executes:

double P = textBox1.Text.ToDouble();
double I = textBox2.Text.ToDouble()/1200;
double M = textBox3.Text.ToDouble();
Cpayment.Text = ((
double)(P * (I/(1 - Math.Pow((1 + I), -M))))).ToString();
VBpayment.Text = Math.Abs(Financial.PMT(I,M,P,0,0)).ToString();

Now, to have access to the Financial.PMT method, in the Solution Explorer you need to add Microsoft.VisualBasic as a reference to your project (see below), and then you may use the using declarative statement as follows:

using Microsoft.VisualBasic;

LVBN2-Vb.net.gif 

Performance vs. Convenience

Although the C# implementation of PMT is a bit better performer than the PMT VB Financial PMT function by a 3 to 8 microseconds in large iteration (10,000 thru 500,000), the cost of development plus maintenance of the C# implementation of all financial functions will outweigh the diminished gain in cycles.

Conclusion:

Having access to .Net languages base classes from any language is a powerful concept.  Experiment with other .Net languages and discover how easy you can leverage the concept of multi-language implementation that .Net has brought to us.


Similar Articles