Home
Map
Console.WriteLinePrint lines to the Console with Console.WriteLine. Call ReadLine and ReadKey to read input.
VB.NET
This page was last reviewed on Feb 18, 2023.
Console. In VB.NET, Console.WriteLine prints a message to the console. And ReadLine will get user input. ReadKey() can handle key presses immediately.
Shows a console
For accessing the console in VB.NET, we use the shared Console class. No instance of Console is needed—we just invoke the functions.
An example. This program uses Console.WriteLine. It prints "Hello world" to the screen. The program is contained in Module1, and 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 .NET.
Shows a console
Module Module1 Sub Main() ' Print hi to be friendly in VB.NET. Console.WriteLine("Hello world") End Sub End Module
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.
Detail 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
Detail The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order.
Format
Detail We can pass any object to WriteLine. When received, the subroutine will call ToString on the Object instance.
ToString
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
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.
Detail Here we write an Integer, a String literal, a Character array, and use a format string with 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
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.
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
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.
Detail We must use the ConsoleColor enum to set colors. Many possible colors, like Red, are available on this enum.
Detail The ResetColor subroutine changes the color scheme (both foreground and background) to the default.
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
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.
Start First we test for the escape key. We use an if-statement and test Key against ConsoleKey.Escape.
Next If we want to test against a char, like the lowercase letter "a," we can access the KeyChar property.
Also We can access the Modifiers property, and test against ConsoleModifiers.Control (or other values) to handle complex key presses.
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
... Press escape, a, then control X ?You pressed escape! aYou pressed a ?You pressed control X
A 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.
C#VB.NETPythonGoJavaSwiftRust
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 18, 2023 (edit).
Home
Changes
© 2007-2023 Sam Allen.