VB.NET Console.Write Program

You want to write text or numbers to the terminal window in your VB.NET program. The Console.Write and Console.WriteLine methods are ideal for this purpose, and they provide many different overloads that you can call.

Console.Write/WriteLine example

Examples

Note

First, this program is divided into two parts: the Test1() and Test2() subs. In the Test1() subroutine, we show how you can use the Console.WriteLine sub with an integer, a string literal, a Char array, a format pattern string, and with no arguments. In the Test2() subroutine, we use the Console.Write sub in the same general ways.

This VB example program demonstrates the Console.Write method.

Program that uses Console.Write and WriteLine [VB.NET]

Module Module1
    Sub Main()
	Test1()
	Test2()
    End Sub

    Sub Test1()
	' 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

    Sub Test2()
	' 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

7
Dot Net Perls
dnp
Dot Net Perls: 999, Awesome

8perls hi!Welcome: 100

Console.WriteLine versus Console.Write. The Console.WriteLine is different from Console.Write because it always appends a newline sequence to each part it writes. You never need to add newlines manually to Console.WriteLine unless you want extra blank lines. Console.Write does not add anything after what you write; you can call Console.Write and Console.WriteLine together to write partial lines and then finish them.

Writing Char arrays. The two array() variables in the program are written to the console with Console.WriteLine and Write. You can specify a range of the array to write, or write the entire array; both examples are demonstrated above.

Format strings. The example shows how you can use format strings with Console.WriteLine and Console.Write. The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order. This is a very clear way to form complex text outputs.

Writing objects

Programming tip

You can pass any object to the Console.WriteLine and Console.Write subroutines. This is because one overload of these methods receives the Object type; when received, the subroutine will internally call the ToString method on the Object instance, resulting in a text representation.

Summary

The VB.NET programming language

Here, we demonstrated the Console.WriteLine subroutine with many different arguments, and also the Console.Write subroutine in a similar way. Many terminal programs combine the two methods for an efficient means of writing text to the screen. For developer-oriented or internal-only programs, console applications are ideal because no graphical elements need to be maintained.

VB.NET Tutorials
.NET