Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 36 ยท Getting Started

Setting Up and Your First Program

Install the tools for Visual Basic .NET, create a console project with Visual Studio or the dotnet CLI, write Hello World and learn how Sub Main, Console and comments work.

To write VB.NET you need two things: the .NET SDK (it contains the Visual Basic compiler and the dotnet command) and an editor. This lesson shows the two common setups and builds your first program. Because this site cannot compile VB, the commands and listings are shown read-only. A runnable C# stand-in at the end lets you play with the same ideas in your browser.

Choose your tools

Tool Good for Notes
Visual Studio (Community is free) Windows Forms, WPF, debugging, designers Windows only. Select the ".NET desktop development" workload.
Visual Studio Code + C# Dev Kit Light editing, command-line workflow Cross-platform, but VB support is basic (colors, no designers).
.NET SDK only Builds, scripts, CI servers Install from the official .NET download page; check with dotnet --version.

If you want to follow the Windows Forms chapter, install Visual Studio. For everything else the SDK and any editor work.

Create a project from the command line

With the SDK installed, open a terminal in an empty folder:

Terminal
dotnet new console -lang "VB" -o HelloVb
cd HelloVb
dotnet run

-lang "VB" selects Visual Basic (the default template language is C#). -o HelloVb creates the folder. dotnet run compiles and starts the program. The template creates HelloVb.vbproj and Program.vb.

In Visual Studio you choose Create a new project, search for "Console App", pick the Visual Basic variant, name it and press Create.

Your first program

The generated Program.vb contains something like this:

VB
Imports System

Module Program
    Sub Main(args As String())
        Console.WriteLine("Hello, World!")
    End Sub
End Module

Take it line by line:

  • Imports System makes the System namespace available so you can write Console instead of System.Console. (Modern project files import System for you automatically, so you often do not need to write it.)
  • Module Program ... End Module declares a module: a container whose members are all shared. Think of it as a static class. You will compare it with classes in Classes and Properties.
  • Sub Main(args As String()) is the entry point. A Sub is a procedure that does not return a value. The runtime calls Main when the program starts. The parameter args holds command-line arguments, and you can leave it out: Sub Main() is also valid.
  • Console.WriteLine("Hello, World!") writes a line of text to the console.
  • Every block is closed by an End statement: End Sub, End Module.

When dotnet run finishes compiling, the console shows Hello, World!.

Reading input and writing output

Console.ReadLine() reads a line the user types and returns a String. Console.Write prints without a line break, and Console.WriteLine adds one.

VB
Module Program
    Sub Main()
        Console.Write("What is your name? ")
        Dim name As String = Console.ReadLine()
        Console.WriteLine("Nice to meet you, " & name & "!")
        Console.WriteLine($"Your name has {name.Length} letters.")
    End Sub
End Module

If the user types Mia, the output ends with Nice to meet you, Mia! and Your name has 3 letters..

Two ways of building text appear here. The & operator concatenates strings (VB's dedicated string operator; you will see why in Operators and Expressions). The $"..." form is string interpolation: expressions inside {} are evaluated and inserted.

Comments and line continuation

A comment starts with an apostrophe ' or with the keyword REM, and runs to the end of the line. XML documentation comments start with three apostrophes and give IntelliSense tooltips.

VB
' This is a comment
Dim total As Integer = 10   ' a comment after code

''' <summary>
''' Adds two numbers.
''' </summary>
Function Add(a As Integer, b As Integer) As Integer
    Return a + b
End Function

Since a statement ends at the end of the line, a long statement needs a continuation. Modern VB continues lines automatically after commas, operators, opening parentheses and a few other places, so you rarely need the old underscore _:

VB
Dim message As String = "Total: " &
                        total.ToString() &
                        " items"

Build, run and debug

Command What it does
dotnet build Compiles the project to bin/Debug/.../HelloVb.dll
dotnet run Builds if needed, then runs
dotnet publish -c Release Produces files ready to deploy
dotnet clean Deletes build output

In Visual Studio press F5 to run with the debugger and Ctrl+F5 to run without it. Click the gray margin next to a line to set a breakpoint; the program pauses there and you can inspect variables, then step with F10 (over) and F11 (into).

Common beginner mistakes

  • Forgetting End Sub or End If. The compiler reports an error such as 'Sub' statement must end with a matching 'End Sub'.
  • Mixing up =. In VB, = is both assignment (x = 5) and comparison (If x = 5 Then). Context decides.
  • Writing Main outside a module or class. The entry point must live inside one.
  • Using + to join text and numbers. Use & to avoid surprises.

Try the same ideas in your browser (C# stand-in)

The runnable block below is C#, not VB. It does exactly what the second VB program does, with the typed name preset so it can run without a keyboard. Press Run, change the name, run again.

C# Stand-in (C#): greeting program

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

A VB Sub that takes parameters and prints looks like this in C#; the concept (a procedure that returns nothing) is the same.

C# Stand-in (C#): a procedure that returns nothing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

In VB.NET this procedure would be Sub Greet(name As String, times As Integer) with a For i As Integer = 1 To times loop. You will write it yourself in Subs and Functions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command creates a Visual Basic console project with the .NET SDK?

    The default language of the console template is C#, so you pass -lang "VB" to get a Visual Basic project.
  2. 2. What is the entry point of a VB.NET console program?

    The runtime starts execution in Sub Main, which usually lives in a Module.
  3. 3. How do you start a comment in VB.NET?

    An apostrophe (or REM) begins a comment that runs to the end of the line.
  4. 4. What does an End statement such as End Sub do?

    VB uses explicit End keywords instead of braces to close blocks. Leaving one out is a compile error.

Summary

  • Install the .NET SDK (and Visual Studio for designers); verify with dotnet --version.
  • dotnet new console -lang "VB" creates a project; dotnet run builds and runs it.
  • A program starts at Sub Main inside a Module; blocks close with End ....
  • Console.WriteLine, Console.Write and Console.ReadLine handle text I/O; & and $"..." build strings.
  • Comments begin with '; the old _ line continuation is rarely needed.

Next: Project Structure and Compilation