Step 5 When we use the zero-argument form of 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
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 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.
Here This program repeatedly calls ReadLine. It tests the input after the return key is pressed.
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 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.
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).