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 an external JavaScript as our dynamic scripting language.This Java Script file will have all the functions that the Calculator will use.

Introduction

In the previous article , we have seen how to invoke a dynamic scripting language (e.g. Java Script) using Iron Python.But in that article, we were building the functions programatically.In this article, we will extend our same example to demonstrate the power of IronJS of invoking an external Java Script file.The development environment is Microsoft Visual Studio Community 2015.

Environment Setup

Let's 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 first create a javascript file say CaliOperations.js which will have the below functions

As can be figure out that, the Call method returns a BoxedValue type. This is a NaN-tagged struct that is used for representing values that don't have a known type at runtime.IronJS passes integers as doubles, so we need to cast them.

Convert.ToDouble(a)

Finally, we are unboxing the unknown values to a known value by invoking the Unbox function

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