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
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor