Section 5.6. Whitespace

   

5.6 Whitespace

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

 Dim myVariable As Integer = 5 

or:

 Dim myVariable As Integer = 5 

and the compiler will treat the two statements as identical.

The exception to this rule is that whitespace within a string is treated as literal; it is not ignored. If you write:

 System.Console.WriteLine("Hello World") 

each space between "Hello" and "World" is treated as another character in the string. (In this case, there is only one space character.)

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. Problems arise only when you do not leave space between logical program elements that require it. For instance, although the expression:

 Dim myVariable As Integer = 5 

is the same as:

 Dim myVariable As Integer=5 

it is not the same as:

 DimmyVariable As Integer = 5 

The compiler knows that the whitespace on either side of the assignment operator is extra, but the whitespace between the access modifier Dim and the variable name myVariable is not extra; it is required.

This is not surprising; the whitespace allows the compiler to parse the keyword Dim rather than some unknown term DimmyVariable. You are free to add as much or as little whitespace between Dim and myVariable as you care to, but there must be at least one whitespace character (typically a space or tab).

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net