Displaying from a Command-Line Program
Updated: March 2009
There are three ways that JScript displays data from a command-line program. The Microsoft JScript command-line compiler provides the print statement. The class Console provides additional methods that facilitate interaction with the user using the console.
The Show method displays information and receives input from pop-up boxes.
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 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 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.