C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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.
VB.NET program that prints line
Module Module1
Sub Main()
' Print hi to be friendly in VB.NET.
Console.WriteLine("Hello world")
End Sub
End Module
Output
Hello world
Array: We can write arrays to the console with WriteLine. We specify a range of the array to write, or write the entire array.
Char ArrayFormat strings: The substitution markers {0} and {1} are replaced in the output with the following arguments in the corresponding order.
String.FormatObjects: We can pass any object to WriteLine. When received, the subroutine will call ToString on the Object instance.
ToStringVB.NET program that uses WriteLine
Module Module1
Sub Main()
' Write an integer line.
Dim value As Integer = 7
Console.WriteLine(value)
' Write a string literal line.
Console.WriteLine("The Dev Codes")
' 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("The Dev Codes: {0}, {1}", 999, "Awesome")
' Write empty line.
Console.WriteLine()
End Sub
End Module
Output
7
The Dev Codes
dnp
The Dev Codes: 999, Awesome
Tip: We can call Console.Write and Console.WriteLine together to write partial lines and then finish them.
Program: Here we write an Integer, a String literal, a Character array, and use a format string with Console.Write.
VB.NET program that uses Console.Write
Module Module1
Sub Main()
' Write integer.
Console.Write(8)
' Write string literal.
Console.Write("Codex ")
' 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
8Codex hi!Welcome: 100
Here: This program repeatedly calls ReadLine. It tests the input after the return key is pressed.
If ThenInfo: 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
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("Download failed")
' Reset the colors.
Console.ResetColor()
Console.WriteLine("Sorry")
End Sub
End Module
Output: color omitted
Warning
Download failed
Sorry
Key: First we test for the escape key. We use an if-statement and test Key against ConsoleKey.Escape.
KeyChar: If we want to test against a char, like the lowercase letter "a," we can access the KeyChar property.
CharPropertyModifiers: We can access the Modifiers property, and test against ConsoleModifiers.Control (or other values) to handle complex key presses.
EnumVB.NET program that uses ReadKey function
Module Module1
Sub Main()
Console.WriteLine("... Press escape, a, then control X")
' Call ReadKey, store result in ConsoleKeyInfo.
Dim info As ConsoleKeyInfo = Console.ReadKey()
' Test for escape.
If info.Key = ConsoleKey.Escape Then
Console.WriteLine("You pressed escape!")
End If
info = Console.ReadKey()
' Test for letter "a" lowercase.
If info.KeyChar = "a"c Then
Console.WriteLine("You pressed a")
End If
info = Console.ReadKey()
' Test for Modifiers Control, and Key.
If info.Key = ConsoleKey.X AndAlso
info.Modifiers = ConsoleModifiers.Control Then
Console.WriteLine("You pressed control X")
End If
End Sub
End Module
Output
... Press escape, a, then control X
?You pressed escape!
aYou pressed a
?You pressed control X