Visual Basic Syntax

Visual Basic has a very simple programming syntax. The language is not case-sensitive and it is designed for people new to the programming world, making it a good place to start.

Go ahead and start a new VB Console Application and give it the name Syntax. You should see the following code:

Module Module1

    Sub Main()
    End Sub

End Module

Your code goes between the Sub Main() and End Sub. In VB, lines does not end with a semi-colon; compared to C# and most other languages, VB is kept simple!

Comments

  • Single line comments start with an apostrophe (‘)
  • There are no block comments in VB
  • XML comments start with three apostrophes; these are useful for documentation purposes

Organizing Code

Organizing code is very important, and comments and regions are very useful. It is considered good practice to group and comment your code so it is easy for other developers to work on it. Comments should provide a small description of that piece of code, and regions are useful use to group code together. To create a region you use the following syntax:

#region "region name"

Other code here..

#End Region

This will be demonstrated further in later tutorials.

Write Your First Program

Now it is time to write your first program. Between the Sub Main() and End Sub enter the following:

Console.WriteLine("My First Visual Basic Program!")

Console.ReadLine()

Hit F5, and you should see the following print out:

My First Visual Basic Program!

Console.WriteLine is a system method.

the period (or dot) is called Member Accessor

Files

When you build your program, certain files are created. It may seem excessive, but they are actually just simple files and instructions for your program and the Visual Studio IDE. Navigate to C:\users\<yourname>\ Documents\Visual Studio\Syntax (or the saved file location).  Find the file called Syntax.sln and a folder called Syntax. If you open the Syntax.sln (sln is a Visual Studio Solution file) with notepad, you will see it has some global settings; we advise you not edit anything!

In the Syntax folder there will be 3 folders: obj, My Project and bin. There will also be two files: Module1.vb and Syntax.vbproj. The Module1.vb  is our source code for our program. Next, open up Syntax.vbproj, which is a Visual Basic Project File. This is actually an XML file which stores information about other file locations; do NOT edit it.

Now open up the bin folder; inside is another folder called debug which is where your program will output. If you double click Syntax.exe the program will run. Go back and open up the My Project folder, and focus on the file called AssemblyInfo.vb. This stores information about the program such as author, copyright, trademark, etc.

Summary

Visual Basic has a very simple programming syntax. Most of the code is easily understandable, even if you are not familiar with programming. The language is not case-sensitive, inline comments start with an apostrophe, and XML comments start with 3 apostrophes