![]() |
JScript.Net Tutorial #1
Once in a while, something happens in the computer industry happens that simply cannot be ignored and when it is created by the biggest software company in the world, everyone has to sit up and take notice. The .NET (pronounced DOT NET) initiative grew out of work being done in the Visual J++ department which had created a set of libraries called WFC (Windows Foundation Classes) allowing Java applications to be compiled for the Windows (and only the Windows) platform. This got up the noses of the creators of Java and so it fizzled out. Then there came C# (pronounced C Sharp) which is a hybrid language based on VB, Java, C and C++ and is a wholly Microsoft created product built to run on the .NET platform created from the WFC. With all the furore over this new language, it seems to be largely forgotten that .Net is language independent and supports a range of languages not just C# or VB provided it is actually implemented. MS themselves have created four languages which include VB.Net, C#, J# and finally JScript.net which is Microsofts own implementation of JavaScript (which incidentally has nothing to do whatsoever with Java). As there doesn't seem to be much support on the web for JScript.Net, I've decided to run this series of tutorials. Why should you use it? Because it could be useful if you are already familiar with JScript (or Javascript) rather than learn C# and it is supported by Visual Studio.Net (although you have to compile from the command line and you can't create winforms visually!).
N.B. This is not to be confused with ASP.Net which can be programmed in JScript (that'll be for another set of tutorials) because ASP.Net is interpreted and this is compiled producing .EXE programs and Jscript.Net is significantly different to Javascript although they share the same heritage. So here we go with our first Jscript.Net program:-
import System;
// A "Hello World!" program in JScript.
Console.WriteLine("Hello World!");
Doesn't look to bad does it? Compare this with a C# program that does the same thing:-
using System;
class hello
{
// This is the usual Hello World! program
public static void Main()
{
Console.WriteLine("Hello World!");
}
}
JScript.Net is certainly a lot less verbose although purists may prefer the C# version as it is better structured. It should be noted that output is to the text console and doesn't produce a GUI window (thats for later). So now you have to compile it and for that you will need a compiler. Now, you're probably getting a bit worried that this is going to cost you money. I mean, surely Microsoft wouldn't give it away for free! Well, actually it seems that they do. The bit you have to pay for is the IDE (Integrated Development Environment) which makes life easier but actually isn't required to write modest sized programs. So where is it? If you have installed version 1.1 of the dotnet framework from your Windows XP CD then you've already got all the compilers you need to follow these tutorials (Win98 users can download the dotnetfx file and install it to run the .NET framework on Win98 and hence use these tutorials. You will also need MDAC 2.7 which is available as a download from the same page but you may already have it if you have been using VB6. You CANNOT run Visual Studio.Net on Win98). You now open a Command Console (in the WinXP Accessories menu) and type:-
path=c:\windows\microsoft.net\framework\v1.1.4322\
dir *.exe
Your version number may be a bit different but the above is correct for my machine running SP2. The dir command will list all the files and amongst it all you will see the following files:-
jsc.exe
vjc.exe
csc.exe
vbc.exe
This opens up some very interesting possibilities. Not only is there a C# compiler (csc.exe) but a Visual J# vjc.exe (the successor to J++), a Visual Basic Compiler (vbc.exe) and finally a Javascript compiler (jsc.exe). This means that you can write .NET applications in Javascript. And it hasn't cost you a penny! So how to use this compiler? Create a batch file (called cc.bat) and fill it with the following commands:-
cd\
cd jsc
path=c:\windows\microsoft.net\framework\v1.1.4322\
csc %1.js
Then create a folder called jsc and put the batch file in it. Create another file called hello.js and put the above JScript code in it. Save and run from the command line:-
jsc hello
and press the return key. It compiles and deposits a hello.exe in the jsc folder. Now just run the hello.exe by typing:-
hello
and the famous words will be displayed. Congratulations, you've created your very first JScript.Net program! It isn't very exciting I admit but its a start.
To summarise the steps.
Now we'll go through the code a line at a time. Bear with me, it is relevent and will help you understand what is to follow when we get to the more interesting stuff.
import System;
This is telling the compiler to include the system library which it needs to compile a console program.
// A "Hello World!" program in JScript.
This is a comment again with a similar syntax to C++ and Java.
Console.WriteLine("Hello World!");
This writes to the console window and the semi-colon closes the main program. And that's all there is to it. Well, alright there is much work to be done to get a GUI window displaying but I'll take you gently through the syntax of the language using console programs and work our way to the GUI stuff.