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

  • You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this webcast that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base application that will jumpstart you into building your own more complex app …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds