Techniques - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Techniques arrow Page 3 - JScript.NET Applications at the Command Line
JScript.NET Applications at the Command Line
By John Mueller

Rate This Article: Add This Article To:

JScript.NET Applications at the Command Line - ' The '
( Page 3 of 3 )

.NET Framework Difference">

Understanding the .NET Framework Difference

Writing JScript at the command line doesn't have to be much different from writing JScript anywhere else. You can get by using any of the containers that support JScript or JavaScript, or even ECMAScript.

ADVERTISEMENT

However, the .NET Framework provides a significant list of objects that you can employ to create your application. You can see a list of these objects here. The JScript specific objects appear here, but you can use any of the objects that appear in the .NET Framework.

Let's look at a little more complex example. Listing 1 shows an example of how you could convert Degrees Fahrenheit to Degrees Celcius at the command line. More importantly, it demonstrates some of the features of JScript.NET.

Listing 1: Converting Temperatures at the Command Line

import System;

// Get the command line arguments.
var CmdArgs = Environment.GetCommandLineArgs();

// Check the number of arguments.
if (CmdArgs.Length < 2)
{
   // Display an error message if there is only one
   // argument.
   Console.WriteLine(
      "Supply the temperature in Fahrenheit.");

   // Exit with an error code.
   Environment.Exit(1);
}

// The user might not provide good input, so it's important
// to provide error trapping.
var InputNum = 0;
try
{
   // Convert the input to a number.
   InputNum = Int32.Parse(CmdArgs[1]);
}
catch(e)
{
   // Display an error message if there is only one
   // argument.
   Console.WriteLine("Supply a numeric value.");

   // Exit with an error code.
   Environment.Exit(2);
}

// Perform the calculation.
var Converted = (InputNum - 32) * 5 / 9;

// Display the result.
Console.WriteLine("{0} degrees F equals {1} degrees C.",
   InputNum, Converted);

The example begins by importing the System namespace, which includes just about everything you need to write a simple utility of the type shown in this section. The Environment.GetCommandLineArgs() method returns an array of strings that contain the command line arguments. The first member of this array is the name of the utility. Starting with array element 1, you see the actual command line arguments. Consequently, when CmdArgs.Length is less than 2 (or equals 1), the user hasn't provided any input. The example displays an error message using the Console.WriteLine() method and then exits using the Environment.Exit() method.

The number provided as input to the Environment.Exit() method appears as an error level that you can test at the command line. For example, you could check it in a batch file and take the appropriate action. Consequently, the technique shown in this section doesn't preclude use of scripts or batch files; you can mix and match if desired.

The next step is to convert any input the user provides to a number. The only problem is that the user might not provide a number. The Int32.Parse() method raises an exception when it encounters something other than a numeric string as input. Consequently, when the user provides something such as "Hello" as input, rather than a number, the try...catch block executes the catch portion of the statement. In this case, the utility displays another helpful message and exits with a different error code.

The example ends by displaying the converted number as output. There's nothing too odd about the actual conversion: the same code can appear in a Web page or as part of a script at the command line. The final Console.WriteLine() method shows just one way of providing output. You don't have to use this approach, but it's probably the easiest method. The {0} and {1} entries in the string are placeholders that receive the variable values, InputNum and Converted.

Creating Your Own Utilities

The technique I've demonstrated in this article isn't the one you should use to write a full-fledged application. Yes, it'll work, but no one needs that much pain in developing something complex. The Visual Studio (or any other) IDE provides useful tools that you need to create applications quickly. However, this technique does work very well for small utility applications that can easily replace the scripts and batch files you use now. In general, you should use this technique to provide better functionality for anyone using your utility and to hide programming details from those who don't have the experience to modify your code.

It's also important to understand that you can produce great .NET applications without using the Microsoft Visual Studio IDE. For example, you might decide to use the Antechinus C# editor. Depending on your goals, you might actually find that these alternatives help you produce better code, faster. Unlike working with C++ or other native code languages, creating a .NET application using a different editor (even Notepad) doesn't mean that you get different output. What you're really getting with an IDE is a set of tools to make development faster and easier.



 
 
>>> More Techniques Articles          >>> More By John Mueller
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.