Visual Basic has a very simple programming syntax. The language is not case-sensitive, and it is easy even for beginners to start coding.
VB.Net program is defined as a collection of objects that communicate via invoking each other's methods.
Let's have a look at a simple code that would print the string "Welcome to VB.NET Tutorial."
Imports System
Module Program
'It will display a string on the console
Sub Main(args As String())
Console.WriteLine("Welcome to VB.NET Tutorial.")
End Sub
End Module
The Sub Main
indicates the entry point of the VB.Net program.
Sub Main(args As String())
Console.WriteLine("Welcome to VB.NET Tutorial.")
End Sub
The following procedure activates the second window in the active document.
Sub MakeActive()
Windows(2).Activate
End Sub
In VB.NET, single-line comments start with an apostrophe ('
).
In the Option Compare statement syntax, the braces and vertical bar indicate a mandatory choice between three items.
Option Compare { Binary | Text | Database }
The following statement specifies that strings will be compared in a sort order that is not case-sensitive within the module.
Option Compare Text
In the Dim statement syntax, the word Dim
is a required keyword. The only required element is varname
(the variable name).
The following statement creates three variables, myVar1
, myVar2
, and myVar3
.
Dim myVar1, myVar2, myVar3
These are automatically declared as Variant variables.
The following example declares a variable as a String
.
Dim myStr As String
To declare several variables in one statement, include the data type for each variable.
Dim x As Integer, y As Integer, z As Integer
Variables declared without a data type are automatically declared as Variant.
In the following statement, x
and y
are assigned the Variant data type. Only z
is assigned the Integer
data type.
Dim x, y, z As Integer