|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Displaying from a Command-Line Program
There are three ways that JScript displays data from a command-line program. The Microsoft JScript command-line compiler provides the print statement. The classprovides additional methods that facilitate interaction with the user using the console.
Themethod displays information and receives input from pop-up boxes.
The print Statement
The most common way to display information is the print statement. It takes one argument, a string, which it displays followed by a newline character in the command-line window.
Either single or double quotation marks can enclose strings, which permits quotes that contain quote marks or apostrophes.
print("Pi is approximately equal to " + Math.PI); print();
The print statement is only available for programs compiled with the JScript command-line compiler. Using print in an ASP.NET page causes a compiler error.
The Console Class
The class Console exposes methods and properties that facilitate interaction with users of the console. The WriteLine method of the Console class provides functionality similar to the print statement. The Write method displays a string without appending a newline character. Another useful method of the Console class is the ReadLine method, which reads a line of text that is entered from the console.
To use classes and methods from the .NET Framework, first use the import statement to import the namespace to which the class belongs. To call the method, use either the fully qualified name or just the name if there is no method in the current scope with the same name.
import System; System.Console.WriteLine("What is your name: "); var name : String = Console.Readline(); Console.Write("Hello "); Console.Write(name); Console.Write("!");
The program requests that a name be entered from the console. After entering the name, Pete, the program displays:
What is your name: Pete Hello Pete!
For more information, see Console.
The Show Method
The Show method is versatile because it is overloaded. The simplest overload has one argument, the string of text you want to display. The message box is modal.
A window or form is modal if it retains the focus until you explicitly close it. Dialog boxes and messages are usually modal. For example, in a modal dialog box, you cannot access another window until you choose OK in the dialog box.
import System.Windows.Forms; System.Windows.Forms.MessageBox.Show("Welcome! Press OK to continue."); MessageBox.Show("Great! Now press OK again.");
You can use other overloads of the Show method to include a caption, other buttons, an icon, or default button. For more information, see Show.