Example: Create and Run a JScript.NET Script

Because Manifold requires installation of Microsoft .NET all of the languages included in .NET or COM are also automatically available, namely C#, JScript, JScript.NET, VB.NET and VBScript.   In this topic we create and run a script using JScript.NET.    For tips on editing the text of scripts, see the Editing Queries, Scripts and Comments topic.

 

Must read info:   All programmers should review the API Documentation online.    The API Documentation provides total details on the API and also provides hundreds of examples, many of which are provided side-by-side in three versions, in C#, in VBScript and in IronPython.

 

In the screen shots below we show the Project pane and other windows in undocked form, to enable more compact illustrations.   

 

 

Right-click into the Project pane and choose Create - New Script.  

 

 

In the New Script dialog, choose JScript.NET in the Language box.

 

 

 

Provide a useful Name for the script.

 

Press Create Script.

 

A new script component of the specified name appears in the Project pane.

 

 

Double-click the new script to open it in a script window.

 

 

 

The new script component opens, loaded with default script text for JScript.NET.   

 

 

We edit the default text to create a script that calculates the factorial of 6, using a classic recursive function that we define.    The new text is:

 

// JScript.NET

class Script

{

static var Manifold: Manifold.Context;

static function factorial(n) {

  return n <= 1 ? 1 : n * factorial(n - 1);

}

static function Main()

{

Manifold.Application.Log(factorial(6));

Manifold.Application.OpenLog();

}

}

 

 

JScript.NET is recursive, so we can utilize the power of recursion to write a simple and elegant factorial function:

 

static function factorial(n) {

  return n <= 1 ? 1 : n * factorial(n - 1);

}

 

We then use that function to calculate the factorial of 6.

 

With the focus on the opened script window, we push the ! Run button in the main toolbar to execute the script.  

 

 

The result is reported in the Log Window.  A quick calculation shows the factorial of 6 is indeed 720.

Using a Message Box

We will now create a script that uses a message box.    

 

We create a new script called Fibonacci and populate it as follows:

 

 

The full script text is:

 

// JScript.NET

class Script

{

static var Manifold: Manifold.Context;

static function fib(a) {

  if (a < 1) return 0;

  if (a == 1) return 1;

  return fib(a-1) + fib(a-2);

}

static function Main()

{

Manifold.Application.MessageBox('fib(7)=' + fib(7));

}

}

 

We compute a Fibonacci number using, once again, recursion:

 

function fib(a) {

  if (a < 1) return 0;

  if (a == 1) return 1;

  return fib(a-1) + fib(a-2);

}

 

We then use that to calculate the Fibonacci number of 7 and output the result to a message box.

 

Manifold.Application.MessageBox('fib(7)=' + fib(7));

 

With the focus on the open script window, press the ! button to run the script.

 

 

As expected, the result is a message box with the seventh Fibonacci number, 13.

 

Notes

Ah, that Google humor... -  An actual screenshot just after pushing the search button...

 

See Also

Tables

 

Queries

 

Editing Queries, Scripts and Comments

 

Scripts

 

Command Window

 

Example: Create and Run a Query -  See how the different parts of a command window operate when creating and running SQL queries.   Includes use of the Log tab as well as the ?expression and !fullfetch commands.

 

Example: Refer to a Table within a Data Source -  Within a query, how to refer to a table that is in a data source.

 

Example: Switching between Manifold and Native Query Engines - How to use the !manifold and !native commands to switch a query in the Command window from use the Manifold query engine to whatever query engine is provided by a data source.

 

Example: Automatically Generating CREATE Queries - How to use the Command window to automatically generate SQL in the form of CREATE queries that create a desired component.

 

Example: VBScript to Create Locations from a Table - Use VBScript to take a table where each record has a name, scale, latitude and longitude and for each record create a Location component in the project.

 

Must read info:   All programmers should review the API Documentation online.