VB.NET Console.WriteLine (Print)

Print lines to the Console with Console.WriteLine. Call ReadLine and ReadKey to read input.
Console. A console program has no graphics. It is text. Easy to develop, it uses few resources and is efficient. Some features, like colors, are available.
A console program will readily accomplish an analytical or processing task. We invoke WriteLine() and Write, and use ReadLine for input. ReadKey() can handle key presses immediately.
An example. This program uses Console.WriteLine. It prints "Hello world" to the screen. The program is contained in a module named Module1. The Sub Main is the entry point of the program.

Tip: We don't need to import any namespaces to access the Console class. It is found in the System namespace in the .NET Framework.

VB.NET program that prints line Module Module1 Sub Main() ' Print hi to be friendly in VB.NET. Console.WriteLine("Hello world") End Sub End Module Output Hello world
Write, WriteLine. This program has 2 parts. In the Test1() sub, we use Console.WriteLine with an integer, a string literal, a Char array, a format pattern string, and with no arguments.

And: In the Test2() subroutine, we use the Console.Write sub in the same general ways.

Array: We can write arrays to the console with WriteLine and Write. We specify a range of the array to write, or write the entire array.

Char Array

Format strings: The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order.


Objects: You can pass any object to WriteLine and Write. When received, the subroutine will call ToString on the Object instance.

VB.NET program that uses Write, WriteLine Module Module1 Sub Main() Test1() Test2() End Sub Sub Test1() ' Write an integer line. Dim value As Integer = 7 Console.WriteLine(value) ' Write a string literal line. Console.WriteLine("Dot Net Perls") ' Write character array range. Dim array(4) As Char array(0) = "d"c array(1) = "n"c array(2) = "p"c array(3) = "x"c array(4) = "x"c Console.WriteLine(array, 0, 3) ' Write format string. Console.WriteLine("Dot Net Perls: {0}, {1}", 999, "Awesome") ' Write empty line. Console.WriteLine() End Sub Sub Test2() ' Write integer. Console.Write(8) ' Write string literal. Console.Write("perls ") ' Write character array. Dim array(2) As Char array(0) = "h"c array(1) = "i"c array(2) = "!"c Console.Write(array) ' Write format string. Console.Write("Welcome: {0}", 100) Console.WriteLine() End Sub End Module Output 7 Dot Net Perls dnp Dot Net Perls: 999, Awesome 8perls hi!Welcome: 100
Console.WriteLine versus Console.Write. The Console.WriteLine sub is different from Console.Write. It always appends a newline sequence to each part it writes. This is sometimes helpful.NewLine

Tip: You never need to add newlines to Console.WriteLine unless you want extra blank lines. Console.Write does not add anything.

And: You can call Console.Write and Console.WriteLine together to write partial lines and then finish them.

ReadLine. Writing is most common with the Console type. But we can also read lines, from the keyboard, with ReadLine. The ReadLine Function returns a String.

Here: This program repeatedly calls ReadLine. It tests the input after the return key is pressed.

If Then

Info: We see if the user typed "1" or "2" and pressed return. We also display the output.

VB.NET program that uses ReadLine Module Module1 Sub Main() While True ' Read value. Dim s As String = Console.ReadLine() ' Test the value. If s = "1" Then Console.WriteLine("One") ElseIf s = "2" Then Console.WriteLine("Two") End If ' Write the value. Console.WriteLine("You typed " + s) End While End Sub End Module Output 1 One You typed 1 2 Two You typed 2 3 You typed 3
Colors. The Windows console supports colors. We can apply foreground (text) and background colors. We assign the ForegroundColor and BackgroundColor properties.

ConsoleColor: We must use the ConsoleColor enum to set colors. Many possible colors, like Red, are available on this enum.

ResetColor: The ResetColor subroutine changes the color scheme (both foreground and background) to the default.

VB.NET program that uses colors Module Module1 Sub Main() ' Set Foreground and Background colors. ' ... You can just set one. Console.ForegroundColor = ConsoleColor.Red Console.BackgroundColor = ConsoleColor.DarkCyan Console.WriteLine("Warning") Console.WriteLine("Download failed") ' Reset the colors. Console.ResetColor() Console.WriteLine("Sorry") End Sub End Module Output: color omitted Warning Download failed Sorry
Console.ReadKey. This function returns immediately when a key is pressed. We can test the result by storing it in a ConsoleKeyInfo structure, and testing its properties.

Key: First we test for the escape key. We use an if-statement and test Key against ConsoleKey.Escape.

KeyChar: If we want to test against a char, like the lowercase letter "a," we can access the KeyChar property.


Modifiers: We can access the Modifiers property, and test against ConsoleModifiers.Control (or other values) to handle complex key presses.

VB.NET program that uses ReadKey function Module Module1 Sub Main() Console.WriteLine("... Press escape, a, then control X") ' Call ReadKey, store result in ConsoleKeyInfo. Dim info As ConsoleKeyInfo = Console.ReadKey() ' Test for escape. If info.Key = ConsoleKey.Escape Then Console.WriteLine("You pressed escape!") End If info = Console.ReadKey() ' Test for letter "a" lowercase. If info.KeyChar = "a"c Then Console.WriteLine("You pressed a") End If info = Console.ReadKey() ' Test for Modifiers Control, and Key. If info.Key = ConsoleKey.X AndAlso info.Modifiers = ConsoleModifiers.Control Then Console.WriteLine("You pressed control X") End If End Sub End Module Output ... Press escape, a, then control X ?You pressed escape! aYou pressed a ?You pressed control X
Print summary. Console.WriteLine and Write can be called with different arguments. There are many overloads available. Often terminal programs combine WriteLine and Write.
Read is useful and called in a similar way. We can output data with colors. And format strings can be used to simplify the syntax of our programs.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls