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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds