TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

VB.NET Console.Write, WriteLine, ReadLine Examples

This VB.NET article demonstrates the Console type. It uses the Console.Write, WriteLine and ReadLine subs.

Console. A console program has no graphics.

It is text. Easy to develop, it uses few resources and is efficient. It will win no visual design awards.

These programs, however, will readily accomplish an analytical or processing task. We invoke WriteLine and Write. We can use ReadLine for input. Numbers and strings are handled.

An example. This program uses Console.WriteLine. It prints "Hello world" to the screen. The program is contained in a module named Module1. The Sub Main is the entry point of the program.

Tip: We don't need to import any namespaces to access the Console class. It is found in the System namespace in the .NET Framework.

Based on:

.NET 4.5

VB.NET program that introduces Console

Module Module1
    Sub Main()
	' Say hi in VB.NET.
	Console.WriteLine("Hello world")
    End Sub
End Module

Output

Hello world

Write, WriteLine. This program has two parts. In the Test1() sub, we use Console.WriteLine with an integer, a string literal, a Char array, a format pattern string, and with no arguments.

Also: In the Test2() subroutine, we use the Console.Write sub in the same general ways.

VB.NET program that uses Write, WriteLine

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("deves ")

	' 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

8deves hi!Welcome: 100

Console.WriteLine versus Console.Write. The Console.WriteLine sub is different from Console.Write. It always appends a newline sequence to each part it writes. This is sometimes helpful.

NewLine

Tip: You never need to add newlines to Console.WriteLine unless you want extra blank lines. Console.Write does not add anything.

And: You can call Console.Write and Console.WriteLine together to write partial lines and then finish them.

Writing Char arrays. We can write array() variables to the console with Console.WriteLine and Write. We specify a range of the array to write, or write the entire array.

Char Arrays

Format strings. The above example uses format strings. The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order.

Tip: This is a clear way to form complex text outputs. More examples are available.

String.Format

Objects. You can pass any object to the Console.WriteLine and Console.Write subroutines. One overload of these methods receives the Object type.

And: When received, the subroutine will internally call the ToString method on the Object instance.

ToString

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.

VB.NET program that uses ReadLine

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

Output

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.

ConsoleColor: We must use the ConsoleColor enum to set colors. Many possible colors, like Red, are available on this enum.

ResetColor: The ResetColor subroutine changes the color scheme (both foreground and background) to the default.

VB.NET program that uses colors

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("There is a disturbance in the Force")

	' Reset the colors.
	Console.ResetColor()

	Console.WriteLine("Sorry")
    End Sub

End Module

Output

Warning
There is a disturbance in the Force
Sorry

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.