JScript .NET, Part X: Displaying Information: Using System.Console - Doc JavaScript
JScript .NET, Part X: Displaying Information
Using System.Console
The class 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 is similar to the one on Page 2; we just replaced the print
statements with calls to System.Console.WriteLine()
(col116ex1.js
):
import System; class HelloWorld{ //constructor function HelloWorld() { Console.WriteLine("Object Constructed"); } function TypeHello() { Console.WriteLine("Hello World."); } } var myHelloWorldObj = new HelloWorld(); myHelloWorldObj.TypeHello();
The following example shows a dialog with the user. It asks for his or her name, and echoes it back:
import System; System.Console.Write("What is your name: "); var name : String = Console.ReadLine(); Console.Write("Hello "); Console.Write(name); Console.WriteLine("!");
Notice the usage of both Write()
and WriteLine()
. Whenever we want to go to the next line, we use the WriteLine()
instead of Write()
. The following command-line window shows the code listing of the above dialog, col116ex3.js
, its compilation, and its execution.
Next: How to use System.Windows.Forms.MessageBox
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 12, 2002
Revised: August 12, 2002
URL: https://www.webreference.com/js/column116/3.html