JScript.net Tutorial

JScript.Net Tutorial #5

Welcome to the fifth JScript.Net tutorial (I really must find a better way to start these tutorials). I want to start with something that is very useful particularly if you want to create console based menus (I know that Windows GUI programming does all that but it is a useful construct all the same) which is called case...switch (if you are familiar with Visual Basic or older versions of BASIC then you will know it as case...select). It allows a decision to be made without using a whole lot of if...else statements as follows:-

using System;import System;

var i : int = 4;

switch(i)
{
	case 1:
		Console.WriteLine("The value is 1");
		break;
	case 2:
		Console.WriteLine("The value is 2");
		break;
	case 3:
		Console.WriteLine("The value is 3");
		break;
	default:
		Console.WriteLine("The value isn't 1, 2 or 3");
		break;
}

Note the use of the break clause. JScript doesn't insist that you use it (unlike C#) but you won't get very far unless you use them as it can lead to some strange errors. So how does it work? An integer value is set up which is coded to contain the number 4. The case statements will therefore branch at the default statement and writeline the relevent value - if it had been set to 1, 2 or 3 then the corresponding writeline will be used and the program will end. Try changing the value of i and recompile and see what happens. This can also be used with strings or characters as follows:-

import System

var c : char = 'H';

switch(c)
{
	case 'h':
		Console.WriteLine("The value is h");
		break;
	case 'H':
		Console.WriteLine("The value is H");
		break;
	case 'e':
		Console.WriteLine("The value is e");
		break;
	default:
		Console.WriteLine("The value isn't h, H or e");
}

Note that the test is case sensitive (although using toUpper or toLower would cure the problem). As I said, a very useful construct. Now on to two different concepts that may at first seem unrelated but they do lead on into the next tutorial. The first is struct.

A struct (shorthand for structure) is not unlike an array in that it can contain data which can then be added to or accessed. This first example creates a structure containing 2 variables then can then be accessed from the main program. The name of the structure is used to create a reference to it which is called personName which creates an instance of the structure in memory. The individual elements of the structure are accessed using a dot followed by the name of the element. So to update the first name we use personName.first followed by the string to enter into it. I wanted to create a structure like I have in the past using standard C as follows:-

struct MyPerson{
char first[25];
char last[25];
} personName[50];

This creates an array of fifty structures with each element fixed to 25 characters each of which can be accessed using:-

personName[i].first="Nick"

so we can have a database of 50 people to call on. Seems quite reasonable that JScript would support this. It doesn't regrettably I can't show you any code to do this although it is possible to simulate it using objects. However, it doesn't really matter as we can use classes to do the same thing which JScript does support. Leaving that aside for the moment, I'll move on to functions.

import System;

function AddItems(Value1, Value2) 
{	
	var ReturnValue : int=null;

	ReturnValue=Value1 + Value2;
	return ReturnValue; 
}

// main program starts here
var k : int=null;

k=AddItems(1,2);
Console.WriteLine(k);

Functions were what made the C programming language one of the most useful and most successful of all time. A function is a little like a subroutine except that information can be passed to it and information can be passed out of it again back to calling program. So here we have a function that will add 2 numbers and return the result of the calculation. Note that while Value1 and Value2 are part of the function declaration, it isn't necessary to declare them inside the function. However, within the function is the ReturnValue which we need to pass back to main program so it is declared. The function values Value1 and Value2 also take on the variable type of the data that is passed to it so Value1 will be an int if a number is passed to it or a string if a string is passed to it. This makes it very flexible although you have to be careful that it doesn't lead to funny errors particularly if a mixture of types is passed to the one function.

Now I have intoduced functions, it is becoming difficult to see where functions end and the main program begins. This isn't so much of a problem with other lngugaes because they have a structured Main() function and JScript seems quite happy to do without it. Hwever, I have discovered it is possible to incorporate a Main() function in Jscript programs so I have rewritten the above function program like this:-

import System;

function AddItems(Value1, Value2) 
{	
	var ReturnValue : int=null;

	ReturnValue=Value1 + Value2;
	return ReturnValue; 
}


public class Test 
{
    	public static function Main() : void 
	{

	var k : int=null;

	k=AddItems(1,2);
	Console.WriteLine(k);
	}
}
// start the Main() program here
Test.Main();

Now it looks much more like a proper programming language with Main() clearly defined. Note however that it jhas to be run at the bottom so it is a bit of a fiddle but it compiles and runs fine so I shall probably stick with it from now on. So thats it with functions and structures. But it is possible to combine them both into...Classes...see you next time...

Back to JScript.Net Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk