August 20, 2002 - Using the System.Console Class
August 20, 2002 Using the System.Console Class Tips: August 2002
Yehuda Shiran, Ph.D.
|
System.Console
is slightly more powerful than the print
statement. Its methods feature display options that are not supported by the print
statement. The WriteLine()
method mimics the print
statement. The Write()
method displays a string without appending a newline character. The ReadLine()
method reads from the command line, starting from the end of the previous output string, and ending at the end of the line. Having both read and write functionalities enables you to create a dialog with the user.
To use classes from the .NET framework, such as the System.Console
above, you need to import the namespace to which the class belongs. Then, when you call a method, you don't need to specify the fully qualified name of the method. Notice again, that the System.Console
class is supported only when you compile your JScript .NET code with the jsc
compiler and run it from the command line. Using the System.Console
class in an ASP.NET page or in Code Behind JScript .NET code will yield a compilation error.
The following JScript .NET script shows how to print messages with the System.Console
class:
import System;
class HelloWorld{
//constructor
function HelloWorld() {
Console.WriteLine("Object Constructed");
}
function TypeHello() {
Console.WriteLine("Hello World.");
}
}
var myHelloWorldObj = new HelloWorld();
myHelloWorldObj.TypeHello();
The program defines a class, HelloWorld
, with two methods: the constructor HelloWorld()
and TypeHello()
. Each method prints one string. The constructor prints the message "Object Constructed"
when an object of type HelloWorld
is constructed. The TypeHello()
method prints the message "Hello World"
. You can compile this program with the jsc
compiler. When you run the generated executable (the output of the compiler), you'll see the two print messages.To learn more about JScript .NET and ASP.NET, go to Column 116, JScript .NET, Part IX: Displaying Information.