ARTICLE

Conditional Compilation in VB.NET

Posted by Mahesh Chand Articles | Visual Basic Language May 07, 2010
Conditional compilation is useful when you need to make some code available based on some conditions.
 
Reader Level:

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.


cc.jpg
Figure 1

Summary

Programming Windows Forms using Visual Basic 2010 is a programming guide for Windows Forms developers using Visual Basic 2010 and Visual Studio 2010. This is an ongoing series. In this part, we learned about the Run and Exit methods of the Application class and how they are used in an application.

 
Programming Windows Forms using Visual Basic 2010

Here are previous articles related to this series.

 

share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor