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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds