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.



About the Author

Mark Strawmyer

Mark Strawmyer is a Senior Architect of .NET applications for large and mid-size organizations. He specializes in architecture, design and development of Microsoft-based solutions. Mark was honored to be named a Microsoft MVP for application development with C# for the fifth year in a row. You can reach Mark at mark.strawmyer@crowehorwath.com.

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds