When writing programs for .NET it works best to put all code into class libraries. This makes it easier to reuse the code in other .NET projects as well as in other environments.
Here I will do a simple four function calculator to show how to set up multiple class library projects in a single solution in Visual Studio.
The following image shows the projects in the Solution Explorer window.

The CsLibDemo is a Windows Application project. The other projects are Class Library projects. When the wizard created the CsLibDemo project it created the Program.cs file and a Windows Form file. I moved the Windows Form file to the CsLibDemoUI project. Any user interface Forms and Control Librairs that are specific to this application can be put into this project. Any user interface classes that are to be used in other applications should be placed in a separate project.
The following image is a screen shot of Windows Explorer, showing how the projects are organized on the hard drive.

The following image is the user interface for testing the class library.

Unit testing is useful for ensuring the proper functioning of your class library. Download NUnit from nunit.org. Here is a screen shot of the NUnit program.

Here is the code in the Calc.cs file.
namespace MyCalc
{
public class Calc
{
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double a, double b)
{
return a - b;
}
public double Multiply(double a, double b)
{
return a * b;
}
public double Divide(double a, double b)
{
return a / b;
}
}
}
Here is the code from CsLibDemoForm.cs
namespace CsLibDemoUI
{
public partial class CsLibDemoForm : Form
{
Calc calc = new Calc();
public CsLibDemoForm()
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
textBoxC.Text = calc.Add(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
}
private void buttonSubtract_Click(object sender, EventArgs e)
{
textBoxC.Text = calc.Subtract(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
textBoxC.Text = calc.Multiply(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
}
private void buttonDivide_Click(object sender, EventArgs e)
{
textBoxC.Text = calc.Divide(double.Parse(textBoxA.Text), double.Parse(textBoxB.Text)).ToString();
}
}
}
Here is the unit test code from CalcTest.cs
namespace CsLibDemo.Tests
{
using System;
using NUnit.Framework;
using MyCalc;
[TestFixture]
public class CalcTest
{
protected double dValue1;
protected double dValue2;
protected Calc calc;
[SetUp] public void Init()
{
dValue1= 6;
dValue2= 3;
calc = new Calc();
}
[Test] public void Add()
{
double result = calc.Add(dValue1, dValue2);
Assert.AreEqual(9, result, "Add");
}
[Test]
public void Subtract()
{
double result = calc.Subtract(dValue1, dValue2);
Assert.AreEqual(3, result, "Subtract");
}
[Test]
public void Multiply()
{
double result = calc.Multiply(dValue1, dValue2);
Assert.AreEqual(18, result, "Multiply");
}
[Test] public void Divide()
{
double result = calc.Divide(dValue1, dValue2);
Assert.AreEqual(2, result, "Multiply");
}
}
}
The unit test code here needs some improvement, but I was keeping it simple for demonstration purposes.
In a future blog I will talk about reusing this class library in LabVIEW.
If anyone has ideas about how to format code in wordpress, or wants more details please leave comments.