Visual Basic, originally called Visual Basic .NET (VB.NET), is a
multi-paradigm
Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms.
Some paradigms are concerned mainly with implications for the execution model of the language, suc ...
,
object-oriented
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
, implemented on
.NET,
Mono
Mono may refer to:
Common meanings
* Infectious mononucleosis, "the kissing disease"
* Monaural, monophonic sound reproduction, often shortened to mono
* Mono-, a numerical prefix representing anything single
Music Performers
* Mono (Japanese b ...
, and the
.NET Framework
The .NET Framework (pronounced as "''dot net"'') is a proprietary software framework developed by Microsoft that runs primarily on Microsoft Windows. It was the predominant implementation of the Common Language Infrastructure (CLI) until bein ...
. Microsoft launched VB.NET in 2002 as the successor to its
original Visual Basic language, the last version of which was Visual Basic 6.0. Although the ".NET" portion of the name was dropped in 2005, this article uses "Visual Basic
NET to refer to all Visual Basic languages released since 2002, in order to distinguish between them and the
classic Visual Basic. Along with
C# and
F#, it is one of the three main languages targeting the .NET ecosystem. As of March 11, 2020, Microsoft announced that evolution of the VB.NET language has concluded.
Microsoft's
integrated development environment
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of at least a source code editor, build automation tools a ...
(IDE) for developing in Visual Basic is
Visual Studio
Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs including web site, websites, web apps, web services and mobile apps. Visual Studio uses Microsoft software development platfor ...
. Most Visual Studio editions are
commercial; the only exceptions are
Visual Studio Express
Microsoft Visual Studio Express is a set of integrated development environments (IDEs) that Microsoft developed and released free of charge. They are function-limited version of the non-free Visual Studio and require mandatory registration. Exp ...
and
Visual Studio Community, which are
freeware
Freeware is software, most often proprietary, that is distributed at no monetary cost to the end user. There is no agreed-upon set of rights, license, or EULA that defines ''freeware'' unambiguously; every publisher defines its own rules for the f ...
. In addition, the
.NET Framework SDK
Microsoft Windows SDK, and its predecessors Platform SDK, and .NET Framework SDK, are software development kits (SDKs) from Microsoft that contain documentation, header files, libraries, samples and tools required to develop applications for Mic ...
includes a freeware
command-line compiler
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
called vbc.exe.
Mono
Mono may refer to:
Common meanings
* Infectious mononucleosis, "the kissing disease"
* Monaural, monophonic sound reproduction, often shortened to mono
* Mono-, a numerical prefix representing anything single
Music Performers
* Mono (Japanese b ...
also includes a command-line VB.NET compiler.
Visual Basic is often used in conjunction with the
Windows Forms
Windows Forms (WinForms) is a free and open-source graphical (GUI) class library included as a part of Microsoft .NET, .NET Framework or Mono Framework, providing a platform to write client applications for desktop, laptop, and tablet PCs. While ...
GUI
The GUI ( "UI" by itself is still usually pronounced . or ), graphical user interface, is a form of user interface that allows users to interact with electronic devices through graphical icons and audio indicator such as primary notation, inste ...
library
A library is a collection of materials, books or media that are accessible for use and not just for display purposes. A library provides physical (hard copies) or digital access (soft copies) materials, and may be a physical location or a vir ...
to make
desktop apps for
Windows
Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
. Programming for Windows Forms with Visual Basic involves dragging and dropping controls on a form using a
GUI designer
A graphical user interface builder (or GUI builder), also known as GUI designer or sometimes RAD IDE, is a software development tool that simplifies the creation of GUIs by allowing the designer to arrange graphical control elements (often cal ...
and writing corresponding code for each control.
Syntax
Visual Basic uses
statements to specify actions. The most common statement is an expression statement, consisting of an
expression to be evaluated, on a single line. As part of that evaluation,
functions or subroutines may be
called and
variables may be
assigned new values. To modify the normal sequential execution of statements, Visual Basic provides several control-flow statements identified by reserved keywords.
Structured programming
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection ( if/then/else) and repetition ( ...
is supported by several constructs including two conditional execution constructs (
If
...
Then
...
Else
...
End If
and
Select Case
...
Case
...
End Select
) and three iterative execution (loop) constructs (
Do
...
Loop
,
For
...
To
, and
For Each
) . The
For
...
To
statement has separate initialisation and testing sections, both of which must be present. (See examples below.) The
For Each
statement steps through each value in a list.
In addition, in Visual Basic:
* There is no unified way of defining blocks of statements. Instead, certain keywords, such as "If … Then" or "Sub" are interpreted as starters of sub-blocks of code and have matching termination keywords such as "End If" or "End Sub".
* Statements are terminated either with a
colon (":") or with the
end of line
End, END, Ending, or variation, may refer to:
End
*In mathematics:
**End (category theory)
**End (topology)
**End (graph theory)
** End (group theory) (a subcase of the previous)
**End (endomorphism)
*In sports and games
** End (gridiron football ...
. Multiple-line statements in Visual Basic are enabled with " _" at the end of each such line. The need for the underscore continuation character was largely removed in version 10 and later versions.
* The
equals sign
The equals sign (British English, Unicode) or equal sign (American English), also known as the equality sign, is the mathematical symbol , which is used to indicate equality in some well-defined sense. In an equation, it is placed between two ...
("=") is used in both assigning values to variables and in comparison.
*
Round brackets (parentheses) are used with
arrays, both to declare them and to get a value at a given index in one of them. Visual Basic uses round brackets to define the parameters of subroutines or functions.
* A
single quotation mark
Quotation marks (also known as quotes, quote marks, speech marks, inverted commas, or talking marks) are punctuation marks used in pairs in various writing systems to set off direct speech, a quotation, or a phrase. The pair consists of an ...
(') or the keyword
REM
, placed at the beginning of a line or after any number of
space
Space is the boundless three-dimensional extent in which objects and events have relative position and direction. In classical physics, physical space is often conceived in three linear dimensions, although modern physicists usually consider ...
or
tab characters at the beginning of a line, or after other code on a line, indicates that the (remainder of the) line is a
comment
Comment may refer to:
* Comment (linguistics) or rheme, that which is said about the topic (theme) of a sentence
* Bernard Comment (born 1960), Swiss writer and publisher
Computing
* Comment (computer programming), explanatory text or informat ...
.
Simple example
The following is a very simple Visual Basic program, a version of the classic "
Hello, World!" example created as a console application:
Module Module1
Sub Main()
' The classic "Hello, World!" demonstration program
Console.WriteLine("Hello, World!")
End Sub
End Module
It prints "''Hello, World!''" on a
command-line window. Each line serves a specific purpose, as follows:
Module Module1
This is a module definition. Modules are a division of code, which can contain any kind of object, like constants or variables, functions or methods, or classes, but can't be instantiated as objects like classes and cannot inherit from other modules. Modules serve as containers of code that can be referenced from other parts of a program.
It is common practice for a module and the code file which contains it to have the same name. However, this is not required, as a single code file may contain more than one module and/or class.
Sub Main()
This line defines a subroutine called "Main". "Main" is the entry point, where the program begins execution.
Console.WriteLine("Hello, world!")
This line performs the actual task of writing the output. ''Console'' is a system object, representing a command-line interface (also known as a "console") and granting programmatic access to the operating system's
standard streams. The program calls the ''Console'' method ''WriteLine,'' which causes the string passed to it to be displayed on the console.
Instead of Console.WriteLine, one could use MsgBox, which prints the message in a dialog box instead of a command-line window.
Complex example
This piece of code outputs
Floyd's Triangle
Floyd's triangle is a triangular array of natural numbers used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:
T ...
to the console:
Imports System.Console
Module Program
Sub Main()
Dim rows As Integer
' Input validation.
Do Until Integer.TryParse(ReadLine("Enter a value for how many rows to be displayed: " & vbcrlf), rows) AndAlso rows >= 1
WriteLine("Allowed range is 1 and ", Integer.MaxValue)
Loop
' Output of Floyd's Triangle
Dim current As Integer = 1
Dim row As Integer
Dim column As Integer
For row = 1 To rows
For column = 1 To row
Write(" ", current)
current += 1
Next
WriteLine()
Next
End Sub
Like Console.ReadLine but takes a prompt string.
Function ReadLine(Optional prompt As String = Nothing) As String
If prompt IsNot Nothing Then
Write(prompt)
End If
Return Console.ReadLine()
End Function
End Module
Comparison with the classic Visual Basic
Whether Visual Basic .NET should be considered as just another version of Visual Basic or a completely different language is a topic of debate. There are new additions to support new features, such as
structured exception handling The Microsoft Windows family of operating systems employ some specific exception handling mechanisms.
Structured Exception Handling
Microsoft Structured Exception Handling is the native exception handling mechanism for Windows and a forerunner te ...
and short-circuited expressions. Also, two important data-type changes occurred with the move to VB.NET: compared to Visual Basic 6, the
Integer