Console
In VB.NET, Console.WriteLine
prints a message to the console. And ReadLine
will get user input. ReadKey()
can handle key presses immediately.
For accessing the console in VB.NET, we use the shared Console
class
. No instance of Console
is needed—we just invoke the functions.
WriteLine
exampleHere we use Console.WriteLine
with an integer, a string
literal, a Char
array, a format pattern string
, and with no arguments.
Console.WriteLine
. A newline is always placed afterwards.string
literals can be passed to WriteLine
as well. This is probably the most common use.WriteLine
. We specify a range of the array to write, or write the entire array.Console.WriteLine
, just an empty line is written to the 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 Module7 Hello World dnp Label: 999, Awesome Goodbye, friends
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.
Console.Write
and Console.WriteLine
together to write partial lines and then finish them.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 Module8perls 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
.
ReadLine
. It tests the input after the return key is pressed.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 Module1 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.
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.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.