Decide what to include at compile time using conditional compiler constants
Takeaway: Conditional compilation constants allow you to control what is included in an application's compiled code. Discover how you can utilize this feature by declaring conditional compiler constants in VB.NET and C#.
There are various scenarios where certain sections of code are not necessary depending upon what you are compiling. For example, you may want to include debugging code during testing but with final application roll-out. Thankfully, C#, VB.NET, and even J# provide this functionally via a set of pre-processing directives. These directives allow you to skip sections of source files for compilation. Let's take a closer look at using this feature.
Making it work
Conditional compilation is utilized by declaring conditional compiler constants in the code. This is achieved with the #Const directive in VB.NET and the #define directive in C#. With the constants declared, blocks of code are defined with the #If..Then..#Else..#End If directive in VB.NET and #if..then..#else..#endif in C#.
The following C# code provides the framework for using this feature (the test method is compiled since the conditional compiler constant Test is defined):
#define test = true
class TestClass {
void test() {
#if test
// do something
#else
// do something else
#endif
}}
Here's the equivalent code in VB.NET:
#Const test = True
Class TestClass
Sub test()
#If test
'do something
#Else
'do something else
#End If
End Sub
End Class
All lines contained by the if statement are enabled, thus included in the compile, and all lines contained by the else block are not included since the constant is true.
Conditional compiler constants are always private to the file in which they appear. There is no way to declare public compiler constants via the #Const directive. In VB.NET, conditional compiler constants and literals are the only value types allowed to be assigned. On the other hand, C#, uses only Boolean value—a constant is true if it is defined. Constants defined with the #Const and #define keywords are only available for conditional compilation; they may not be used anywhere else in the code. One interesting caveat is the constants may be undefined if not explicitly declared before being referenced by an if statement. In this scenario, a Nothing value is assigned in VB.NET and null in C#. Listing A contains a simple example. Listing B contains the equivalent code in C# (notice that it uses a Boolean value instead of the integer).
The result of these sample applications produces the following output:
Values passed in: x = 12, y = 10
Calculated value = 66
Return value: 66
Resetting the constants to a non-zero value for the VB.NET sample and false for the C# code results in the following code:
Return value: 66
Resetting the value in VB.NET is as simple as assigning the necessary value, but C# behaves a bit different. The #undef directive sets a constant to false, so the following code would accomplish setting the test constant to false. The constant is false by default (if it is not defined) so the code would produce the equivalent output if the #define test line was removed. See Listing C.
You can create and use your own constants to control what is included in your compiled code, but there are reserved constants as well.
Reserved constants
Three conditional compilation constants are provided: Config, Debug, and Trace. Debug and Trace are Boolean data types. If you're using the Visual Studio .NET IDE, the Project Properties dialogue allows them to be defined. When Debug is defined, Debug class methods generate output to the Output window. If it is not defined, Debug class methods are not compiled and no Debug output is generated. Also, when Trace is used, Trace class methods generate output to the Output window. If it is not defined, Trace class methods are not compiled and no Trace output is generated. Finally, the Config constant is a string data type. It corresponds to the value assigned in the Configuration Manager window in Visual Studio .NET. If you are not using Visual Studio .NET, you can include the constants in the top of the code.
Compile only the code you need
The preprocessor conditional compilation constants provided by the .NET languages VB.NET and C# allow you to easily control what is contained in the compiled version an application. It makes it easy to include debugging and other extra code during development and testing yet keeping it out of the final compiled version. After all, reducing the amount of code and processing in the final version leads to a leaner and meaner product.
Miss a column?
Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.
SponsoredWhite Papers, Webcasts, and Downloads
- Does fragmentation affect SANs, NAS, and RAID? Diskeeper
- Live Webcast: Simplified IT with Software-as-a-Service (SaaS) ZDNet
- The Shortcut Guide to Managing Disk Fragmentation - Chapter 1 Diskeeper
- Self-Tuning Disk Drives Eliminate Performance Bottlenecks and Heighten ROI Diskeeper
- Defrag Myth Busters - What You Should Know Diskeeper
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
