Skip to content
LumenaryAcademy
Foundations 50 min

First steps with Visual Basic

From BASIC to Visual Basic 6 to Visual Basic .NET, where the language stands today, and your first programs.

Code tested · 3AI-draftedAwaiting review by a computer scientist
MotivationH

Why learn Visual Basic now?

Most languages in this catalogue are taught because you might start a new project in them. Visual Basic is different. Few teams start large new systems in it today, and Microsoft has said plainly that the language will not gain new features. Yet an enormous amount of Visual Basic code is running right now: inventory systems in warehouses, laboratory instruments' control software, insurance and banking back offices, school administration tools, engineering calculators, and, through VBA, millions of Excel workbooks and Access databases that whole departments depend on.

That code needs people who can read it, fix it, keep it secure, move it to modern .NET, and, when the time comes, migrate it carefully to another language without breaking the business that relies on it. Visual Basic is also genuinely pleasant to read: it was designed to look like English, so that people who were not professional programmers could write useful software. Learning it well, including its quirks and its history, is a practical skill and a good lesson in how languages age.

History & global context

From BASIC to Visual Basic .NET

BASIC (Beginner's All-purpose Symbolic Instruction Code) was created at Dartmouth College in 1964 by John Kemeny and Thomas Kurtz so that students in every subject could use a computer. In the home-computer era of the late 1970s and 1980s, a BASIC interpreter came built into almost every machine, and Microsoft's first product was a BASIC. QuickBASIC later brought structured programming (procedures, no line numbers) to DOS.

Visual Basic 1.0 appeared in 1991. Its big idea came partly from a prototype by the designer Alan Cooper, which Microsoft bought and combined with its BASIC: you drew a window, dragging buttons, text boxes and lists onto a form, and then wrote a few lines of code in event handlers that ran when the user clicked or typed. Building a Windows program suddenly took hours instead of weeks. Successive versions added database access, components, classes and native compilation, and Visual Basic 6 (1998) became one of the most widely used development tools of its time, especially for in-house business software. VBA, the same language embedded in Microsoft Office, made Excel, Word and Access programmable.

In 2002 Microsoft replaced classic Visual Basic with Visual Basic .NET, a new language on the new .NET Framework. It kept the familiar keywords but became fully object-oriented, with inheritance, structured exception handling and free threading, and it compiled to the same intermediate language as C#. It was not compatible with VB6: real programs needed substantial rewriting, and many organisations kept running VB6 applications for decades. Visual Basic .NET then grew alongside C# for years, gaining generics (2005), LINQ and XML literals (2008), Async and Await (2012), string interpolation and null-conditional operators (2015) and tuples (2017).

Where it stands today. In March 2020 Microsoft announced that Visual Basic would be supported on .NET 5 and later, for Windows Forms, WPF, console applications and class libraries, and stated that it did not plan to evolve Visual Basic as a language. Its current language strategy describes Visual Basic as having a stable design: the .NET libraries keep supporting it, and when new runtime or C# features need language support, Visual Basic takes a "consumption-only" approach (it can use types built with those features, without new syntax to define them), and it will not be extended to new kinds of application. In practice that means Visual Basic is fully supported and runs on the latest .NET, but new web front ends, cross-platform user interfaces and the newest language ideas are C# territory. The compiler in the .NET 10 SDK supports Visual Basic language version 17.13.

VB6 and VBA live on separately. The VB6 development environment has long been out of support, though Microsoft has continued to ship the VB6 runtime with Windows so that existing programs keep working. VBA remains part of Microsoft Office on Windows and macOS and is still heavily used for spreadsheet automation; newer alternatives include Office Scripts (TypeScript) and add-ins built with web technologies.

ComputationC

Your first Visual Basic program

Install the .NET SDK from the official download page; Visual Basic is included. Visual Basic has no single-file mode like C#'s dotnet run file.cs, so every program lives in a project:

bash
dotnet new console -lang VB -o Hello
cd Hello
dotnet run

dotnet new creates Hello.vbproj and a Program.vb. Replace Program.vb with:

vb
Option Strict On

Module Program
    Sub Main()
        Console.WriteLine("Hello, world!")
        Console.WriteLine("Sawubona, mhlaba!")
    End Sub
End Module

dotnet run builds and runs it:

text
Hello, world!
Sawubona, mhlaba!

The pieces:

  • Option Strict On must be the first line of the file. It is explained below, and you should write it at the top of every file (or set it once in the project file).
  • Module Program ... End Module declares a module: a container for procedures and data that are shared by the whole program. (Under the hood, a module is a class whose members are all shared.)
  • Sub Main() is the entry point, the procedure where the program starts. A Sub is a procedure that does something and returns no value.
  • Console.WriteLine prints a line, exactly as in C#: Visual Basic uses the same .NET library.
  • Blocks end with explicit End keywords (End Sub, End Module, End If) rather than braces, and there are no semicolons: each statement ends at the end of its line.

The project file, Hello.vbproj, looks like this:

xml
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>Hello</RootNamespace>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>

</Project>

Many teams set the OptionStrict property to On in the project file's PropertyGroup, which turns Option Strict on for every file at once. Visual Basic projects also import a set of common namespaces automatically (System, System.Collections.Generic, System.Linq and others), so most programs need few Imports lines.

A slightly larger program

vb
Option Strict On

Imports System.Globalization

' A module holds procedures and data shared by the whole program.
Module Program
    ' Sub Main is where the program starts.
    Sub Main()
        Dim name As String = "Ngozi"
        Dim visits As Integer = 3
        Dim lastVisit As Date = #11/14/2026#     ' date literals are always month/day/year

        Console.WriteLine("Welcome back, " & name & "!")
        Console.WriteLine($"This is visit number {visits + 1}.")
        Console.WriteLine($"Last time: {lastVisit.ToString("d MMMM yyyy", CultureInfo.InvariantCulture)}")

        ' Keywords and names are not case-sensitive: these all mean the same variable.
        VISITS = visits + 1
        Console.WriteLine($"Visits now: {Visits}")

        ' Long statements can continue on the next line after an operator or a comma.
        Dim message As String = "Thank you for being " &
                                "a regular visitor."
        Console.WriteLine(message)
    End Sub
End Module

It prints:

text
Welcome back, Ngozi!
This is visit number 4.
Last time: 14 November 2026
Visits now: 4
Thank you for being a regular visitor.
  • Comments start with an apostrophe ' and run to the end of the line.
  • Dim name As String = "Ngozi" declares a variable with a type and an initial value. Dim comes from "dimension", a relic of early BASIC that has stuck.
  • & joins strings; it converts its operands to text, so "Total: " & 15 works. Interpolated strings start with $, as in C#.
  • Date literals are written between # signs and always use the American month/day/year order, whatever the computer's regional settings. #11/14/2026# is 14 November 2026.
  • Visual Basic is not case-sensitive. visits, VISITS and Visits are the same variable, and so are Dim and DIM. The Visual Studio editor quietly corrects the case of every name to match its declaration; other editors do not, so keep the case consistent yourself.
  • Line continuation: a statement can continue on the next line after an operator, a comma or an opening parenthesis. Older code ends such lines with a space and an underscore, _, which is still allowed.
DefinitionsH

Option Strict, Explicit and Infer

Three settings, written at the top of a file or set for the whole project, change how strict the compiler is.

Definition— Option Explicit

When On (the default), every variable must be declared with Dim (or similar) before it is used. With it Off, a mistyped name silently creates a new variable. Never turn it off.

Definition— Option Strict

When On, the compiler forbids implicit narrowing conversions (ones that could lose information, such as Double to Integer or String to Integer) and late binding (calling members on an Object whose type is only known at run time). When Off, which is the default for compatibility with older code, it inserts those conversions and run-time look-ups for you.

Definition— Option Infer

When On (the default), Dim count = 3 gives count the type of its initial value, here Integer, just like C#'s var. The variable is still statically typed.

Here is what Visual Basic does with Option Strict Off:

vb
Option Strict Off
Option Infer On

Module Program
    Sub Main()
        ' With Option Strict Off, Visual Basic converts silently, sometimes surprisingly.
        Dim quantity As Integer = "42"          ' a string becomes a number
        Dim half As Integer = 7 / 2             ' 3.5 is rounded to the even neighbour: 4
        Dim total As Integer = quantity + half
        Console.WriteLine($"quantity={quantity} half={half} total={total}")

        ' Late binding: the method is looked up while the program runs.
        Dim something As Object = "late"
        Console.WriteLine(something.ToUpper())

        ' Option Infer: the type comes from the initial value.
        Dim price = 19.99D
        Dim count = 3
        Console.WriteLine($"{price.GetType().Name} {count.GetType().Name} {price * count}")
    End Sub
End Module

It prints:

text
quantity=42 half=4 total=46
LATE
Decimal Int32 59.97

Each line compiles, and each hides a decision. The string "42" was parsed into a number; if it had been "forty-two", the program would have crashed at run time. 7 / 2 is 3.5 (in Visual Basic / always divides as floating point), and assigning it to an Integer rounded it, to the even neighbour 4. something.ToUpper() was looked up while the program ran, so a typo in the method name would compile and fail later. With Option Strict On, the first of these lines does not compile at all:

text
error BC30512: Option Strict On disallows implicit conversions from 'String' to 'Integer'.

and you must say what you mean: Integer.Parse("42"), CInt(7 / 2), or a variable of the right type. Professional Visual Basic code uses Option Strict On everywhere, and turning it on in an old codebase, one file at a time, is one of the first things a maintainer does. The D suffix in 19.99D makes a Decimal literal; Int32 is the .NET name of Visual Basic's Integer.

ExplanationH

Where you will meet Visual Basic

If you work with Visual Basic professionally, you will most likely meet it in one of these forms:

  • Windows Forms and WPF desktop applications, often written for one organisation's internal use: order entry, scheduling, stock control, reporting. Many started on the .NET Framework and are being moved to modern .NET, which supports both frameworks with Visual Basic.
  • Class libraries that hold business rules and are shared by several applications, sometimes now called from C#.
  • ASP.NET Web Forms sites written in VB.NET on the .NET Framework. Web Forms does not exist on modern .NET, so these are usually maintained as they are or rewritten, often in C#.
  • Office automation: VBA macros inside workbooks and databases, and VB.NET add-ins that extend Office.
  • VB6 applications still running in production, sometimes for decades, now usually candidates for replacement.

The tools follow the history. Visual Studio on Windows gives by far the best Visual Basic experience, with IntelliSense, refactoring, the Windows Forms designer and the debugger; other editors offer limited support. The dotnet command line builds, tests and runs Visual Basic projects on any operating system, which is how the programs in this course were checked, and continuous-integration systems build them like any other .NET project. Knowing your way around both the designer-centred Windows workflow and the command line is part of maintaining such code well.

Common misconceptionsH

Common misconceptions

Misconception

"Visual Basic .NET is just VB6 with a new name." They share keywords and spirit, but VB.NET is a different language on a different platform: true classes and inheritance, exceptions instead of On Error, a different type system, the .NET libraries. Code rarely moves between them without rewriting.

Misconception

"Visual Basic is deprecated and will stop working." It is fully supported on current .NET, including .NET 10, and existing programs keep compiling and running. What Microsoft has stopped is the evolution of the language and its spread to new kinds of application.

Misconception

"Visual Basic is slow because it's a beginner's language." VB.NET compiles to the same intermediate language as C# and runs on the same JIT compiler; equivalent code performs the same. Slowness in old VB code usually comes from Option Strict Off and late binding, which add run-time work.

Misconception

"VBA and Visual Basic .NET are the same thing." VBA is the classic, VB6-era language hosted inside Office, with its own editor and object model. It cannot use .NET libraries directly, and VB.NET code does not run inside Office as a macro.

Exercises

Exercises

Each program assumes the console project from this lesson; paste it into Program.vb and use dotnet run to check your answer.

1. First steps with Visual Basic

Warm-upComputational

What does this program print?

Option Strict On

Module Program
    Sub Main()
        Dim total As Integer = 10
        TOTAL += 5
        Dim label As String = "Total: " & Total
        Console.WriteLine(label)
    End Sub
End Module

2. First steps with Visual Basic

CoreComputational

With Option Strict Off, what does this program print?

Option Strict Off

Module Program
    Sub Main()
        Dim a As Integer = 5 / 2
        Dim b As Integer = 7 / 2
        Console.WriteLine(a + b)
    End Sub
End Module

3. First steps with Visual Basic

CoreComputational

What is the second line printed by this program?

Option Strict On

Module Program
    Sub Main()
        Console.WriteLine("1" & 2 & 3)
        Console.WriteLine(1 + 2 & 3)
    End Sub
End Module

4. First steps with Visual Basic

CoreHuman

Which of these lines are compile errors under Option Strict On? (Choose all that apply.)

Choose all that apply

5. First steps with Visual Basic

Warm-upHuman

Which statements about Visual Basic today are accurate? (Choose all that apply.)

Choose all that apply

6. First steps with Visual Basic

Warm-upHuman

On a computer set to British regional settings (day/month/year), which date does the literal #03/04/2026# mean in Visual Basic?

Choose one

Further reading and resources