Home
VB.NET
Console.WriteLine
This page was last reviewed on Nov 14, 2023.
Dot Net Perls
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.
Console Color
WriteLine example. Here we use Console.WriteLine with an integer, a string literal, a Char array, a format pattern string, and with no arguments.
Step 1 We can write values like Integers to the screen with Console.WriteLine. A newline is always placed afterwards.
Step 2 Strings and string literals can be passed to WriteLine as well. This is probably the most common use.
Step 3 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
Step 4 The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order.
String.Format
Step 5 When we use the zero-argument form of Console.WriteLine, just an empty line is written to the console.
Shows a console
Module Module1 Sub Main() ' Step 1: write an integer line. Dim value As Integer = 7 Console.WriteLine(value) ' Step 2: write a string literal line. Console.WriteLine("Hello World") ' Step 3: 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) ' Step 4: write format string. Console.WriteLine("Label: {0}, {1}", 999, "Awesome") ' Step 5: write empty line. Console.WriteLine() Console.WriteLine("Goodbye, friends") End Sub End Module
7 Hello World dnp Label: 999, Awesome Goodbye, friends
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.
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
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
ToString. Suppose we have an Object in our VB.NET program and pass it to Console.WriteLine or Write. The ToString function is used by Console.WriteLine to print it out.
ToString
Console.ReadKey. We can use ReadKey for an immediate response that does not require the Enter key to be pressed. This can be useful for interactive VB.NET programs.
Console.ReadKey
Summary. Console.WriteLine, Write and Read are often used in VB.NET programs. We can output data with colors. And format strings can be used to simplify the syntax of our programs.
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 Nov 14, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.