Calculator Application Using IronJS and WPF

Rajnilari2015
Posted by in WPF category on for Beginner level | Points: 250 | Views : 2511 red flag

In this article, we will build a simple calculator by using WPF and will use IronJS for writing the various calculator operations.We are using JavaScript as our dynamic scripting language.


 Download source code for Calculator Application Using IronJS and WPF

Introduction

IronJS is an ECMAScript 3.0 implementation built on top of the Dynamic Language Runtime from Microsoft which allows us to embed a javascript runtime into the .NET applications.The author of this project is Fredrik Holmstrom.In this article, we will build a simple calculator by using WPF and will use IronJS for writing the various calculator operations.We are using JavaScript as our dynamic scripting language.The development environment is Microsoft Visual Studio Community 2015.

Environment Setup

Lets create a solution file and add two project, one WPF(CalciPL) and another Class Library(CalciBL) projects. Choose the Class Library(CalciBL) project, click on Managed Nuget Packages.

In the Search box Type "IronJS" and click "Search"

Click on the Install button to install "IronJS". We are using version 0.2.0.1

Once installed, we will get the below screen

Now we are good to go for writing our Business Logic(s)

Using the code

Let us write the below code in the CaliOperation.cs file

using System;

namespace CalciBL
{
    public class CaliOperation
    {
        public int CaliOperations(int a, int b, string @operator)
        {
            // Instantiate the IronJS Engine
            var context = new IronJS.Hosting.CSharp.Context();

            string expression = string.Empty;
            
            //Build the expression as per the operator choosen
      switch(@operator)
      {
           case "+":
           expression = "(function(){ return " + a.ToString() + "+" + b.ToString() + " ;})()";
           break;

           case "-":
           expression = "(function(){ return " + a.ToString() + "-" + b.ToString() + " ;})()";
           break;

           case "*":
           expression = "(function(){ return " + a.ToString() + "*" + b.ToString() + " ;})()";
           break;

           case "/":
           expression = "(function(){ return " + a.ToString() + "/" + b.ToString() + " ;})()";
           break;
     }

         //Execute method runs the dynamic code
         return Convert.ToInt32(context.Execute(expression));
      }
    }
}

Code Explanation

The very first thing we need to perform is to instantiate the IronJS Engine which can be achieve by the below code

new IronJS.Hosting.CSharp.Context()

Next, based on the operator choosen at runtime, we are building the expression.And finally we are executing the dynamic expression by using the Execute method of the IronJS Engine

Since the Execute method returns an object, we are typecasting that to the type we need

To test our funciton, we can write a simple Unit Test project as under

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CalciBL.Tests
{
    [TestClass()]
    public class CaliOperationTest
    {
        [TestMethod()]
        public void CaliOperationAdditionSuccessTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 60;
            var actual = objCalciOp.CaliOperations(20, 40, "+");
            Assert.AreEqual(expected, actual);
           
        }

        [TestMethod()]
        public void CaliOperationAdditionFaliureTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 35;
            var actual = objCalciOp.CaliOperations(-20, 40, "+");
            Assert.AreNotEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationSubtractionSuccessTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = -20;
            var actual = objCalciOp.CaliOperations(20, 40, "-");
            Assert.AreEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationSubtractionFaliureTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 35;
            var actual = objCalciOp.CaliOperations(-20, 40, "-");
            Assert.AreNotEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationMultiplicationSuccessTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = -800;
            var actual = objCalciOp.CaliOperations(-20, 40, "*");
            Assert.AreEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationMultiplicationFaliureTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 35;
            var actual = objCalciOp.CaliOperations(20, 40, "*");
            Assert.AreNotEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationDivisionSuccessTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 10;
            var actual = objCalciOp.CaliOperations(400, 40, "/");
            Assert.AreEqual(expected, actual);

        }

        [TestMethod()]
        public void CaliOperationDivisionFaliureTest()
        {
            CaliOperation objCalciOp = new CaliOperation();
            var expected = 35;
            var actual = objCalciOp.CaliOperations(56, 40, "/");
            Assert.AreNotEqual(expected, actual);

        }
    }
}

Now let us make our UI design in WPF which will be as under

And the code is as under

using CalciBL;
using System;
using System.Windows;

namespace CalciPL
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        CaliOperation objCalciOp;
        public MainWindow()
        {
            InitializeComponent();
            objCalciOp = new CaliOperation();
        }

 private void btnAdd_Click(object sender, RoutedEventArgs e)
 {
  var nums = InputValues();
  txtResult.Text = Convert.ToString(objCalciOp.CaliOperations(nums.Item1, nums.Item2, "+"));
 }

private void btnSub_Click(object sender, RoutedEventArgs e)
{
    var nums = InputValues();
    txtResult.Text = Convert.ToString(objCalciOp.CaliOperations(nums.Item1, nums.Item2, "-"));
}

private void btnMul_Click(object sender, RoutedEventArgs e)
{
 var nums = InputValues();
 txtResult.Text = Convert.ToString(objCalciOp.CaliOperations(nums.Item1, nums.Item2, "*"));
}

private void btnDiv_Click(object sender, RoutedEventArgs e)
{
   var nums = InputValues();
   txtResult.Text = Convert.ToString(objCalciOp.CaliOperations(nums.Item1, nums.Item2, "/"));
}

       private Tuple<int, int> InputValues()
        {
            var num1 = Convert.ToInt32(txtNum1.Text);
            var num2 = Convert.ToInt32(txtNum2.Text);
            return Tuple.Create(num1, num2);
        }
    }    
}

Now run the application and we can feel the taste of our simple calculator.

Conclusion

In this article we have seen how to invoke a dynamic language like JavaScript using IronPython.Hope this will be helpful.Attached is the zipped file.Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)