Posts Tagged ‘C#’

sourceforge

February 3, 2010

I have created a sourceforge project called Doodle Space which I will use to upload complete source code for this blog. Doodle Space is at http://sourceforge.net/projects/doodlespace/.

Use your SVN client to find the source code at https://doodlespace.svn.sourceforge.net/svnroot/doodlespace.

I use TortoisesSVN, which can be found at http://tortoisesvn.tigris.org/.

So 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 directory.

Now all I have to do is find some time to upload examples from my old website and write some new ones.

C# Class Libraries

November 29, 2009

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.


Follow

Get every new post delivered to your Inbox.