Previous section   Next section

3.5 Whitespace

In the Visual Basic .NET language, spaces and tabs are considered to be "whitespace" (so named because you see only the white of the underlying "page"). Extra whitespace is generally ignored in Visual Basic .NET statements. Thus, you can write:

myVariable = 5

or:

myVariable    =                        5

and the compiler will treat the two statements as identical. In fact, Visual Studio .NET will automatically discard the extra white space and close up the second version so that it resembles the first!

The exception to this rule is that whitespace within strings is not ignored. If you write:

Console.WriteLine("Hello World")

each space between "Hello" and "World" is treated as another character in the string.

Most of the time the use of whitespace is intuitive. The key is to use whitespace to make the program more readable to the programmer; the compiler is indifferent.

However, there are instances in which the use of whitespace is quite significant. Although the expression:

Dim x As Integer = 5

is the same as:

Dim x As Integer=5

it is not the same as:

Dimx As Integer = 5

The compiler knows that the whitespace on either side of the assignment operator is extra, but the whitespace between the keyword Dim and the identifier x is not extra, and is required. This is not surprising; the whitespace allows the compiler to parse the keyword Dim rather than some unknown term Dimx. You are free to add as much or as little whitespace between Dim and x as you care to, but there must be at least one whitespace character (typically a space or tab).


  Previous section   Next section
Top