Language Fundamentals

 <  Day Day Up  >  

If you take a look at the source code for the HelloWorld program from the previous chapter, several things should already be obvious. The first thing that should stand out is the way that the Visual Basic .NET language takes advantage of English words and syntax to make code as readable as possible. Compare the following Visual Basic .NET version of the "Hello, world!" program:

 Module HelloWorld   Public Sub Main()     Console.WriteLine("Hello, world!")     Console.ReadLine()   End Sub End Module 

with the equivalent program in Visual C# .NET.

 using System; class HelloWorld {   public static void Main()   {     Console.WriteLine("Hello, world!");     Console.ReadLine();   } } 

In many ways the syntax of the two programs looks similar. But a closer look at how the Main subroutine is defined reveals some differences ”in Visual Basic .NET, the subroutine Main starts with the statement Sub Main and ends with the statement End Sub . In Visual C# .NET, the subroutine Main starts with the statement void Main and an opening curly brace ( { ) and ends with a closing curly brace ( } ). While Visual C# .NET emphasizes conciseness at the cost of some readability, Visual Basic .NET emphasizes readability at the cost of some conciseness. This emphasis on using English (or English-like) words and syntax extends throughout the language and is intended to make reading Visual Basic .NET code as easy as possible. In most cases, keywords in the language describe what they do, and statements can be read as if they were sentences.

Case Insensitivity

Another aspect of Visual Basic .NET that is different from many other programming languages is that it is case insensitive . That is, the language does not care whether characters are uppercase or lowercase in a program. The following code compiles just the same as the original HelloWorld program shown earlier.

 module HelloWorld   SUB Main()     console.writeline("Hello, world!")     ConsolE.ReadlinE()   EnD SuB END MODULE 

The fact that module is not capitalized has no effect on the program, nor does the fact that END is all uppercase.


Although the language is very relaxed in regard to casing, it is always a good idea to use consistent casing because this increases the readability and understandability of code. The Visual Studio IDE will usually automatically correct casing to be consistent.

In the same way that the language is not strict about casing, the language is also relaxed in regard to parts of the language that can be understood implicitly. For example, the following version of the HelloWorld program is also equivalent to the first example.

 Module HelloWorld   Sub Main     Console.WriteLine("Hello, World!")     Console.ReadLine   End Sub End Module 

This version of the HelloWorld program leaves off the parentheses after the Sub Main and Console.ReadLine statements. This is allowed because it can be reasonably inferred that leaving off the parentheses means that the subroutine has no parameters or arguments ”the same thing that specifying empty parentheses indicates. There are many places in the language where pieces of syntax are optional when the intent is obvious.


In general, it is best to specify syntax even if it is optional. The Visual Studio IDE will usually insert optional syntax if it is omitted.

Line Orientation

Another fundamental aspect of Visual Basic .NET may have become obvious if you tried typing in the HelloWorld program and didn't type it in exactly as it appeared on the page. Unlike some programming languages, Visual Basic .NET is line-oriented . This means that where a line ends is significant ”lines cannot end just anywhere . This is in contrast to other languages, such as C++, that are free-format . Free-format languages allow ending lines almost anywhere the programmer pleases, even in the middle of a statement (although not in the middle of a word).


Visual Basic .NET's line orientation is a byproduct of the past ”because the BASIC language was originally an interpreted (rather than compiled) language, the interpreter needed to know when to start interpreting the next statement. Although Visual Basic .NET is not an interpreted language, it still retains this design point from its ancestors .

Except in the cases discussed later in the chapter, Visual Basic .NET does not allow more than one statement on a line and does not allow a statement to span more than one line. For example, although the follow program looks almost the same as the first HelloWorld program, it will not compile.

 ' This program will not compile! Module HelloWorld   Sub Main   (   )     Console.WriteLine("Hello, world!")     Console.       ReadLine         () End Sub End Module 

Compiling the preceding program will cause many different errors because Visual Basic .NET expects that an entire statement will appear on one line and that each statement will appear on its own line. The statement Sub Main() starts the Main subroutine, so the language expects that the entire statement will be together. Similarly, the End Sub statement ends the Main subroutine, so the language expects that it will be the only statement on that line.

When you have a very long line of code, requiring that an entire statement be on one line can be painful. To help with this, a program can use line continuations to break a single statement across multiple lines. A line continuation allows a statement to be extended over one or more lines by placing an underscore ( _ ) at the end of a line. This tells the compiler that the line continues on to the next line. It is important to notice that there has to be at least once space between the line continuation and anything that comes before it.


There has to be one space before a line continuation because the underscore can also be part of a name . Without this rule, given a line that looks like x = y_ , it would be unclear whether the underscore was part of the name y_ or a line continuation.

It is also possible to put more than one statement on a line by using a statement separator . A statement separator separates the statements on a line by placing a colon ( : ) between each statement. The one restriction on statement separators is that a subroutine or function declaration must always be the first statement on a line. For example, the following code is still not valid, because the Sub Main statement must be the first statement on a line.

 ' This program will not compile! Module HelloWorld : Sub Main()     Console.WriteLine("Hello, world!") End Sub : End Module 

The original example, though, could be made to compile using line continuations and statement separators.

 Module _ HelloWorld   Sub Main _   ( _   )     Console.WriteLine("Hello, world!")     Console. _       ReadLine _         () End Sub : End Module 

The line continuation characters keep the entire Module and Sub declaration statements together, while the statement separator separates the two End statements.


A comment is text that is intended solely for the programmer and not for the compiler. Comments can be used to provide descriptions of what code does or to serve as reminders of things that still need to be done. Comments in Visual Basic .NET are started by using the single quote character ( ' ) anywhere on a line and continue from that point until the end of the line. The language ignores anything in a comment, so a comment may contain any text. For example:

 ' ' This program prints the string "Hello, world!" to the output ' window. ' ' ' This contains the Main routine and does all the work. ' Module HelloWorld   '   ' This is the main routine and prints out the string.   '   Public Sub Main()     Console.WriteLine("Hello, world!")  ' Print the string     Console.ReadLine()                  ' Wait for input   End Sub  ' Main End Module  ' HelloWorld 

Since everything after a comment begins is ignored, it is not possible to continue a comment across multiple lines. Comments that will not fit on one line must be broken into multiple lines with each line starting with a single quote, as in the preceding example. Comments cannot be placed after a line continuation.

 ' This program will not compile! Module HelloWorld   Sub Main( _           ' Invalid comment     Args() As String _  ' Invalid comment     )                   ' Valid comment     Console.WriteLine("Hello, world!")     End Sub End Module 

The second and third comments in this example are not valid comments, because they follow a line continuation. The last comment is valid because it does not.


In keeping with historical precedent, the word REM can also be used to start a comment, but its use is discouraged.

 <  Day Day Up  >  

The Visual Basic .NET Programming Language
The Visual Basic .NET Programming Language
ISBN: 0321169514
EAN: 2147483647
Year: 2004
Pages: 173
Authors: Paul Vick

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: