C# Class Libraries

By wburris

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.

SolutionExplorer

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.

WindowsExplorer

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

Calc

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.

NUnit

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.


Tags: , , , , , ,

3 Responses to “C# Class Libraries”

  1. Rahul Says:

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    http://CSharpTalk.com

    Thanks again
    Rahul

  2. Naveen Says:

    HI

    I am Naveen.I had learnt ,how to run selenium IDE & selenium RC.The main thing is how to generate a Test Report after the running the test Succesfully.I am using the Selenium RC and NUNIT for the testing .How to get the Test actions and descriptions in a Report.And another thing is How to run Selenium Core and when it is useful .And suggest some more open source tools for testing .Net and Java Projects ………………………..

    Try to help me…..

    Thanks
    Naveen

  3. sourceforge « Bill's Electronics & Software Development Notes Says:

    [...] far it only contains the CsLibDemo folder, which is the code for the C# Class Libraries, and Using .NET in LabVIEW blog entries. Download the contents of the trunk [...]

Leave a Reply