Visual Basic Syntax

Visual Basic has a very simple programming syntax the language is not case-sensitive, so something like Thecodingguys and are the same thing. The programming language is really for people new to the programming world, and VB is the place to start since it is simple.

Go ahead and start a new VB Console Application, 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 or anything, unlike 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, comments and regions are very useful. it is considered good practice to group and comment your code so it's easy for other developers to work on it (No developer want's to open a huge document with no comments!). Comments should provide a small description of that piece of code. On the other hand regions are also useful use them to group code together. To create a region you use the following syntax

#region "region name"

Other code here..

#End Region

You will see how useful regions can be in later tutorials.

Write Your First Program

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, 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 a lot, but actually they are just simple files and instructions for your program and the Visual Studio IDE. Navigate to C:\users\<yourname>\ Documents\Visual Studio\Syntax (or where ever you saved the work).  In there is a 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 just see it has some global settings, my advice to you is don't edit anything!

Open up the Syntax folder and in there will be 3 folders, obj, My Project and bin. There will also be two files Module1.vb and Syntax.vbproj, open up the Module1.vb file and you will see that is just our source code for our program. Then open up Syntax.vbproj which is just a Visual Basic Project File, it is actually an XML file which stores information about other file locations do NOT edit it.

Now open up the bin folder in there is another folder called debug this is where your program will output. If you double click Syntax.exe you will see the program run. Go back and open up the My Project folder focus on the file called AssemblyInfo.vb this just 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're not familiar with programming (that's why it's called Basic!). The language is not case-sensitive, inline comments start with an apostrophe, XML comments start with 3 apostrophe's