Preprocessing

 <  Day Day Up  >  

Several special statements can be used in a source file to control compilation, even though they are not considered part of the code. These statements are called preprocessing statements because they are processed before the code is actually compiled. All preprocessing statements begin with the pound sign (#) at the beginning of a line.

Conditional Compilation Statements

The principle preprocessing statements are the conditional compilation statements , which allow code to be selectively compiled based on conditions external to the code. For example, you may wish to compile some code only if you are building an application for debugging purposes.

 Module Test   Sub Main() #If DEBUG Then     MsgBox("Application is starting!") #End If     Console.WriteLine("Hello, world!") #If DEBUG Then     MsgBox("Application is ending!") #End If   End Sub End Module 

In the preceding example, the two message boxes will appear only if the DEBUG conditional compilation constant is True . The #If statement has almost the same syntax as a regular If statement. For example:

 Module Test   Sub Main() #If DEBUG Then     MsgBox("Debug application") #ElseIf RETAIL Then     MsgBox("Retail application") #Else     MsgBox("Unknown type of application") #End If     Console.WriteLine("Hello, world!")   End Sub End Module 

Conditional compilation constants are defined using #Const statements or through the Visual Studio environment. The #Const statement assigns a value to a conditional compilation constant. Conditional compilation constants are always typed as Object ; it is not possible to specify a type in a #Const statement. For example:

 #Const DEBUG = True Module Test   Sub Main() #If DEBUG Then     MsgBox("Debug application") #ElseIf RETAIL Then     MsgBox("Retail application") #Else     MsgBox("Unknown type of application") #End If     Console.WriteLine("Hello, world!")   End Sub End Module 

Conditional compilation constants do not have to be defined before they can be used ”the previous examples will compile even if DEBUG and RETAIL are never defined. The scope of a constant is from the #Const statement through the end of the file. If a constant is used before being defined, the value of the constant is Nothing . A conditional constant can be assigned to more than once.

 #Const TEST = True #If TEST Then Module Test   Sub Main()     Console.WriteLine("Hello, world!")   End Sub End Module #End If #Const TEST = False 

Region Statements

The other preprocessing statement is the #Region statement, which is used to mark text in the Visual Studio code editor but has no effect on compilation. A #Region statement can be followed by a string literal that describes the region. #Region statements are block statements that must be ended by #End Region statements. For example:

 Module Test #Region "The Main method of the application"   Sub Main()     Console.WriteLine("Hello, world!")   End Sub #End Region End Module 

#Region statements cannot appear within methods or property accessors and can only enclose valid blocks. In other words, #Region statements must span an entire block and cannot break in the middle.

 Module Test   ' Error: Invalid #Region statement #Region "The Main method of the application"   Sub Main()     Console.WriteLine("Hello, world!") #End Region   End Sub End Module 
 <  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

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