Your First Program
Prerequisites: Setting up your environment
Replace the contents of Program.vb with:
Module Program
Sub Main()
Console.WriteLine("Hello, world!")
Console.WriteLine("Welcome to Visual Basic.")
End Sub
End Module
Run it with dotnet run.
Expected output
Hello, world!
Welcome to Visual Basic.
Line by line:
Module Program ... End Module— a module is a container for code. Every VB statement lives inside a module or class.Sub Main() ... End Sub—Mainis the entry point: the procedure that runs when the program starts. ASubis a procedure that doesn't return a value.Console.WriteLine(...)— prints text followed by a new line.Console.Writeprints without the new line.- Strings go in double quotes. There are no semicolons: each statement is one line.
Case and spacing don't matter
VB is not case-sensitive: console.writeline works the same as
Console.WriteLine, and Visual Studio fixes the capitalization for you.
Indentation is for readability only.
Building a line in pieces
Module Program
Sub Main()
Console.Write("Loading")
Console.Write("...")
Console.WriteLine(" done")
Console.WriteLine("Name: " & "Ada" & ", age: " & 36)
Console.WriteLine($"Interpolated: {2 + 3}")
End Sub
End Module
Loading... done
Name: Ada, age: 36
Interpolated: 5
& joins strings (and converts numbers to text). $"..." is an
interpolated string: expressions in braces are evaluated and
inserted.
Practice
Write a program that prints your name on one line, and on the next line
"I am learning Visual Basic" built with & from two pieces.
Show solution
Module Program
Sub Main()
Console.WriteLine("Ada")
Console.WriteLine("I am learning " & "Visual Basic")
End Sub
End Module
Ada
I am learning Visual Basic