Home Contact Us Site Map
Sunday, March 18, 2018
Article written by Samuel Mela

Visual C# Express - Separate Data From Display. (3rd In A Series)

In my last article, "Visual C# Express: Projects and Solutions", I used the example of an interest calculator program to demonstrate how a C# Windows program can be constructed by putting multiple projects into a solution.

The solution, called called "InterestCalculatorSolution", contains a Windows Application Project called "InterestCalculator", and a Class Library Project called "FinancialLib". This solution is a simple example of how data is separated from presentation in modern visual programming.

Form Classes and Data Classes

The heavy lifting in the InterestCalculator program is done by two classes:

CompoundGrowth handles presentation in the InterestCalculator. It is a C# class based on the .net Framework "form" class. In object oriented terms, CompoundGrowth is said to "inherit" the .net Framework "form" class. A "form" class is basically a blank form. The CompoundGrowth class is based on a blank form but adds a a button and some text input boxes so a user can input financial information, and an output text box so the program can display the results of interest growth calculations. CompoundGrowth contained in the "InterestCalculator" project.

cFinancial handles calculations. It uses principal, interest rate, time, and compounding schedule to calculate the growth of an investment. cFinancial is a C# class, but it is not based on any other C# class. cFinancial is contained in the "FinancialLib" project.

Who's In Charge?

Every C# solution needs a "Startup Project". In the InterestCalculatorSolution, the InterestCalculator project is the startup project. Initially the program loads loads the CompoundGrowth form and presents it to the user, but the cFinancial class is not be called until the user actually presses the "Calculate Amount" in the CompoundGrowth form.

In other words, the cFinancial class is a slave to the CompoundGrowth class. In fact, the cFinancial class is only used in one routine, btnCalculateAmount_Click, which is called when the " Calculate Amount" button is pressed.

The code to use the cFinancial is quite simple, I have reproduced it below, with the comments changed slightly for this article:

// Create a financial object from the cFinancial class
cFinancial financial = new cFinancial();

// Load it with data
financial.Principal = mPrincipal;
financial.Rate = mRate/100.0;
financial.Years = mYears;
financial.N = mN;

// Calculate the new value of the investment
mAmount = financial.Amount();


As you may have figured out by now, the cFinancial class is highly reusable. If you look at the code in the cFinancial class, you will notice that it contains no references to the CompoundGrowth form. If you replaced the CompoundGrowth form with a completely different form, the cFinancial class would not have to be changed. That is one of the features of a good code library -- it has no specific knowledge of the software that is using it.


You can find a copy of the Interest Calculator Code on my Article Support Page.

In this article I discussed how C# solutions can be divided into data handling projects and presentation (display projects. In the InterestCalculator program the InterestCalculator project handles presentation and the FinancialLib project handles data. The FinancialLib project creates a data handling library that any other presentation project could use without modification.
Related Searches

data handling, calculator interest, calculator interest simple, calculator interest rate, calculator interest investment, calculator interest only, calculator compounding interest