On CBSSports.com: Bargaining in a lousy economy

Decide what to include at compile time using conditional compiler constants

Tags: .NET, Programming languages, Microsoft development tools, Tony Patton, directive, conditional compiler, Microsoft Visual Basic.Net, C#, compiler, compilation, .NET Newsletter

  • Save
  • Print
  • 5

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).

Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

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.

  • Save
  • Print
  • 5

What do you think?

advertisement
Click Here