Conditional Compilation in VB.NET
Conditional compilation is useful when you need to make some code available based on some conditions.
Conditional Compilation
Conditional compilation is useful when you need to make some code available based on some conditions and code snippet in Listing 4 places three statements in a single line. Sometimes for testing purposes or debug purposes, you only want to run certain line of code only when debugging the application. We can simply have a conditional directive and make it true only in case of debugging and make it false one application is all tested.
The class listed in Listing 5 has two constants DEBUG and TESTING. When value of these directives is True, then only Enabled method will be available.
Public Class ConditionalCompilationClass
#Const DEBUG = False
#Const TESTING = False
#If DEBUG Then
Sub DebugEnabledMethod()
Console.WriteLine("Debuging Enabled. ")
End Sub
#Else
Sub DebugDisabledMethod()
Console.WriteLine("Debuging Disabled. ")
End Sub
#End If
#If TEST Then
Sub TestingEnabledMethod()
Console.WriteLine("Testing Enabled. ")
End Sub
#Else
Sub TestingDisabledMethod()
Console.WriteLine("Testing Disabled. ")
End Sub
#End If
End Class
Listing 5
Since value of these directives is false, if you create an instance of ConditionalCompilationClass, you will see only two Disabled methods are available in Intellisense. See Figure 1.
Figure 1