Console programs do not have the flash, nor the event-driven capabilities of a Windows application, however, they still have their place. For example, where screen space is limited such as a bank ATM, or a device with a very simple display type.
Console.WriteLine ("string to write to console")As we have seen in our error example, to write the string "Hello World!" to the console, we use the statement :
Console.WriteLine ("Hello World!")It should be noted that the WriteLine method also writes a carriage return/line feed (CRLF) following the string argument. The similar Console.Write method behaves as does the WriteLine method except without the CRLF.
The class method most frequently used to perform console input is the ReadLine method. The ReadLine method reads the next line of characters from the input stream and assigns these to the variable via the assignment operator. The Console.ReadLine method returns a String data type. When using the ReadLine method, the program will pause and wait until the Enter key is pressed before continuning (i.e. waits for data to be input). The generic format for the ReadLine method is:
string_variable = Console.ReadLine ()Data input can be combined with type conversion (as previously discussed) to form a single line statement (although there are still 2 parts to the single line statement). An example looks as follows:
decInput = Convert.ToDecimal (Console.ReadLine())The Console.ReadLine statement performs the data input, then passes the input String data directly to the Convert method, which performs the conversion and assigns the converted data to the Decimal variable. Notice there is no intermediate String variable for the ReadLine, the data flows directly from the ReadLine into the Convert method.
1 The Console class belongs to the System namespace. Since the System
namespace is the top most hierarchical namespace, this namespace is imported by default so the System can be omitted.
Problem Solving Revisited
The steps in the example are numbered with comments and should be easily understood based upon previous section explanations. Note that step 2 (input and conversion) appears twice; once using two steps (commented out), and one in one combined step. It should be obvious that step two should only be performed once, using one technique or the other. Also of note, the WriteLine method in step 4 using a String operator as well as a String method, both of which are easily explained here. Make sure you can relate the steps below to those listed in the list above (especially #3)!
Option Explicit On Option Strict On 'program name: NumberDoubler 'program to read a decimal number from the console and double it Module NumberDoubler Sub Main() '0. declare 2 variables, one for input, one for result Dim decNumToDouble As Decimal Dim decResult As Decimal '1. prompt for input Console.Write("Enter number to double: ") '2. read input and convert to decimal - two steps 'strInput = Console.ReadLine() 'decNumToDouble = Convert.ToDecimal(strInput) '2. read input and convert to decimal - steps combined decNumToDouble = Convert.ToDecimal(Console.ReadLine()) '3. double the input value decResult = decNumToDouble * 2 '4. output doubled value Console.WriteLine("Number doubled is: " & decResult.ToString) 'dummy readline to keep console window alive Console.ReadLine() End Sub End Module |
Next Section: Windows Programming | Table of Contents |
©2007, Mark A. Thomas. All Rights Reserved.