Sharing Code Part III: Conditional Compile

In the first part of Sharing Code series we looked at sharing a common class source file across three different platforms, and in the second part we continued with using partial classes to separate the code out. In this third part of the series, we’ll look at using conditional compiles in our code and continue working on the sample code from the previous articles.

What is Conditional Compile?

Conditional compile is essentially telling the compiler to either compile a piece of code or to ignore it. For example the following will compile the code if it is a DEBUG build but this code will not compile in RELEASE build.

#if DEBUG
     System.Diagnostics.Debug.WriteLine("test output");
#endif

DEBUG and RELEASE are pre-defined in your project, but you can also define your own directives as follows

#define MyDirective

#if MyDirective
    System.Diagnostics.Debug.WriteLine("test output");
#endif

Whenever you define your directive, you will have to define it at the beginning of your CS file or you can define it in your project properties in the Build tab as follows

10

Now that we have a overview on conditional compiles, we can look at how to implement in the sample code.

Sharing the UI Code

If you have been following along in the series, you may have noticed that we have some common code in the MainPage.xaml.cs. Using a combination of conditional compiles and partial classes, we will change it so we have a common code base.

In the WP7_App project, add a class called MainPage_Common.cs with the following implementation

namespace WP7_App
{
    public partial class MainPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            btnAdd.Click += (o, e) =>
            {
                lblResult.Text = Common.Math2.Default.Add(this.X, this.Y).ToString();
            };

            btnSubtract.Click += (o, e) =>
            {
                lblResult.Text = Common.Math2.Default.Subtract(this.X, this.Y).ToString();
            };

            btnMultiply.Click += (o, e) =>
            {
                lblResult.Text = Common.Math2.Default.Multiple(this.X, this.Y).ToString();
            };

            btnDivide.Click += (o, e) =>
            {
                lblResult.Text = Common.Math2.Default.Divide(float.Parse(x.Text), float.Parse(y.Text)).ToString();
            };

            System.Diagnostics.Debug.WriteLine(Common.DeviceInfo.Default.DeviceId);
        }

        private int X
        {
            get
            {
                return Int32.Parse(x.Text);
            }
        }

        private int Y
        {
            get
            {
                return Int32.Parse(y.Text);
            }
        }
    }
}

Delete the constructor, the X & Y properties from MainPage.xaml.cs. Run the project and essentially it will do the same thing as previously.

Updating Windows Phone 8 Project

In the WP8_App project, add the MainPage_Common.cs file to the project making sure to add the file as a link. Delete the constructor, the X & Y properties from MainPage.xaml.cs. When you try and compile the project, you will get around 16 various errors. Our main culprit is the following

namespace WP7_App

Which is the namespace used in our Windows Phone 7 project. To fix this, we can use conditional compile statements. Open up the WP8_App project properties and in the Build tab set the Configuration to All Configurations and add WP8 to the Conditional Compile Symbols. Your project properties should look like the following

11

Now open MainPage_Common.cs and add the following for the namespace

#if WP8
namespace WP8_App
#else
namespace WP7_App
#endif

Trying to compile the code, you will get an error which is a result of no Math2.Divide() method taking floats as a parameter. This method is only available in the WP7_App project and is by design. To fix this we can again add a conditional compile.

Back in WP7_App project, open the project properties and add a conditional compile called WP7. In MainPage_Common.cs change the btnDivide.Click implementation to the following

#if WP7
                lblResult.Text = Common.Math2.Default.Divide(float.Parse(x.Text), float.Parse(y.Text)).ToString();
#else
                lblResult.Text = Common.Math2.Default.Divide(this.X, this.Y).ToString();
#endif

Compile and run the code on both and you will not get anymore compile time errors.

Updating the Windows 8 Project

Updating the Windows 8 project is essentially the same as the Windows Phone 8 project. You will have to

  1. Add the MainPage_Common.cs as a link to the project
  2. Remove the constructor in MainPage.xaml.cs
  3. Remove the X & Y properties in MainPage.xaml.cs
  4. Add a W8 as a Conditional Compile symbol in the project build properties
  5. Add the following to MainPage_Common.cs to allow the code to compile
#if WP8
namespace WP8_App
#elif W8
namespace W8_App
#else
namespace WP7_App
#endif

Now when you run the projects you will get the same results. But the advantage now is you have a common code base across all three platforms to drive your app logic.

Up Next

In the next part, we’ll look at using Portable Class Libraries or managed DLLs across various platforms.

 

 


Warning: count(): Parameter must be an array or an object that implements Countable in /home/usnbis1maldq/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405

2 thoughts on “Sharing Code Part III: Conditional Compile”

Leave a Reply