Monday, September 22, 2008

Using compiler directives with VB.NET

With VB.NET Microsoft introduced compiler directives to Visual Basic language. Compiler directives have been around for a long time in other languages and compilers and now they are also part of Visual Basic.

There are four compiler directives in VB.NET:

  • #Const directive
  • #ExternalSource directive
  • #If...Then...#Else directive
  • #Region directive

ExternalSource directive

Directive is used for mapping between specific lines of source code and external source text. See Microsoft's reference for information about this directive.

Region directive

With #Region "name"...#End Region directive you can organize source code to the blocks in the editor. These blocks have "+"-sign to "open" the block and "-"-sign to "hide" the block. When a block is hidden, only the "+"-sign followed by region's name are visible. This is very handy if the source file is long. You can put for example all helper routines to "helper"-region and form's event handlers to "form"-region. When blocks are collapsed, all you see is "helper" and "form" titles instead of lots of source code.

Const directive

This directive is commonly used in conjunction with #If directive. With #Const directive you can assign compiler variables which you can test with #If...Then...#Else directive.

#Const directives can be set from Project Properties to be global constants i.e. they are available in all project's modules. However, this is not possible with VB.NET Express Edition. If you are using Express Edition, #Const directive is only available in the module where it is defined.

If...Then...Else directive

#If...Then...#Else directive is the most flexible and the most useful VB.NET compiler directive. Below are a few samples for what you can do with this directive.

Conditional compiling:

#Const TargetOS = "Vista"

#If TargetOS = "Vista" Then
   ' Windows Vista specific code
#ElseIf TargetOS = "WinXP" Then
   ' Windows XP specific code
#Else
   ' Code for other OS
#End if

With conditional compiling you can write operating system, processor specific or other target platform specific code to the same source file. Changing #Const directive value and recompiling code gives you platform specific executables without maintaining multiple source codes for the same application.

Simple localization:

#Const Language = "French"

#If Language = "French" Then
   ' Show message to user in French
#Else
   ' Show message to user in English
#End if

If you have a simple user interface in your application or otherwise do not need a full localization, you can make a simple localization with conditional compiling.

Debugging code in source code:

#Const DebugMode = True

Public Sub DoSomething(ByVal ArgNumber As Integer)
#If DebugMode = True Then
  Debug.WriteLine("Entered sub DoSomething with argument: " & ArgNumber)
#End If
  ' Rest of procedure code

End Sub 

When you are on the development and coding phase of the application, you usually have more or less code for debugging purpose. Debugging code is something that you must never leave in the final production code. And debug dumps are something that your customers should never see. Again, conditional compiling is an easier solution than maintaining multiple source codes for the same application.

No comments: