Conditional Compile Statements

In this weekly article we’re going to focus on another topic that is often
under utilized, which is conditional compile statements and how to use them in
your code.B The most common place I use these is when I have multiple versions
of my application.B For example, let’s say I have a full feature version as
well as a trial version of my application.B I define multiple solution
configuration profiles and compile my code under the desired profile.B
Conditional compile statements used in code surround code that is activated or
not based on the active configuration.B They are extremely useful regardless if
you are a C#
developer
doing C#
programming
whether it be for ASP.NET
developer, WCF,
or general .NET
Framework
developer.

Configuration Manager

An important step in using conditional compile statements is recognizing
that Visual
Studio
has a Configuration Manager that lets you define as many
solution configuration profiles as you would like.B By default, projects have a
Debug and a Release configuration.B You can use those as a starting point to
modify, or create your own.B You can launch Configuration Manager a number of
ways.B One of which is by going to the properties on your solution, clicking
Configuration Properties, and clicking the Configuration Manger button.B Refer
to Figure 1, where you’ll see that I used the Configuration Manager to create a
new configuration called TRIAL, which is a copy of Debug.B

Visual Studio - Configuration Manager
Figure 1: Visual Studio – Configuration Manager

The properties of each project also have a Build tab where you can control
whether the DEBUG or TRACE constants are defined, whether code is optimized,
warning levels, etc.B

Using Conditional Compilation in Code

The following section will demonstrate some sample code that is wrapped in
conditional compile statements.B The statements govern whether or not the code
is considered part of the project during compile based upon the active
configuration.B In this particular example, I have a multiple tenant ASP.NET
application.B In production the application determines which tenant is running
based on the URL.B When in development, I use different solution configurations
to govern which tenant is considered active.B The following code is an indexer
on an application settings object that pulls cached settings from memory based
on the tenant being used.B It isn’t as important that you understand this
particular code, but rather see how conditional compile statements can work to
your advantage.

public string this[string name]
{
    get
    {
string settingKey = "";
      int tenantID = 0;

#if (CARD)
      tenantID = 1;
      settingKey = "AppSettings1";
#elif (CTRAC)
      tenantID = 2;
      settingKey = "AppSettings2";
#elif (AMR)
      tenantID = 3;
      settingKey = "AppSettings3";
#else
      tenantID = AppUrls.GetCurrentTenantId();
#endif

      // Make sure we have an AppSettings cache item loaded for the tenant
      settingKey = string.Format("AppSettings{0}", tenantID.ToString());
      if (HttpContext.Current.Cache[settingKey] == null)
      {
      	App.AppSettings.LoadAppSettings(tenantID);
      }

      Hashtable ht = (Hashtable)HttpContext.Current.Cache[settingKey];
      if (ht.ContainsKey(name))
      {
      	if (ht[name] != null)
            {
            	return ht[name].ToString();
            }
            return string.Empty;
      }
      else
      {
            return string.Empty;
      }
   }
}

About the Author

Mark Strawmyer is a Senior Architect of .NET
applications for large and mid-size organizations. Mark is a technology leader
with Crowe Horwath LLP in
Indianapolis, Indiana.B He specializes in architecture, design and development
of Microsoft-based solutions.B Mark was honored to be named a Microsoft MVP for
application development with C# again.B You can reach Mark through http://markstrawmyer.com.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read