A directive modifies programs in an early stage of compilation. The #Const directive defines a value. And the #If directive can test those constants.
Other directives, like #Region, are also available. Directives add a layer of compile-time logic to VB.NET programs. They can make programs harder to read.
This program uses the #Const directive to set the SPECIAL symbol to the value 1. Inside the Main
Sub
, we use an #If directive to test the value of SPECIAL.
#Const SPECIAL = 1 Module Module1 Sub Main() ' Local variable. Dim a As Integer = 1 ' Use special value is SPECIAL is 1. #If SPECIAL = 1 Then a = 2 #End If ' Display value. Console.WriteLine(a) End Sub End Module2
Another directive available in VB.NET programs is #Region. This directive partitions a code file. It has no effect on the compiled program.
Module Module1 #Region "INIT" ''' <summary> ''' Initialize the program. ''' </summary> Sub Main() Console.WriteLine("A") Console.WriteLine("B") Console.WriteLine("C") Console.WriteLine("Copyright 2017") End Sub #End Region End ModuleA B C Copyright 2017
Preprocessing directives are handled at an early stage of compilation. The preprocessor scans VB.NET programs before compiling code statements into intermediate language.
Directives add another layer of compilation to VB.NET programs. With #If, #Const and even #Region, we access another set of abilities in this language.