VB.NET Console.WriteLine (Print)

Print lines to the Console with Console.WriteLine. Call ReadLine and ReadKey to read input.

dot net perls

Console. Imagine a message written in the clouds above. In VB.NET, meanwhile, we do not write on clouds—instead we focus on terminal windows inside.

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 do not 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

WriteLine example. Here we use Console.WriteLine with an integer, a string literal, a Char array, a format pattern string, and with no arguments.
Array: We can write arrays to the console with WriteLine. 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: We can pass any object to WriteLine. When received, the subroutine will call ToString on the Object instance.
VB.NET program that uses WriteLine Module Module1 Sub Main() ' 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 End Module Output 7 Dot Net Perls dnp Dot Net Perls: 999, Awesome

Write example. Unlike WriteLine, Console.Write() does not append a newline to the console after the data. This Sub is the same as WriteLine in other ways. It seems to be less often used.
Tip: We can call Console.Write and Console.WriteLine together to write partial lines and then finish them.
Program: Here we write an Integer, a String literal, a Character array, and use a format string with Console.Write.
VB.NET program that uses Console.Write Module Module1 Sub Main() ' 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 8perls hi!Welcome: 100

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-2020 sam allen. send bug reports to info@dotnetperls.com.