Debug.Assert replacement for Phone and Store apps

Sun, January 13, 2013, 09:42 PM under MobileAndEmbedded

I don’t know about you, but all my code is, and always has been, littered with Debug.Assert statements. imageI think it all started way back in my (short-lived, but impactful to me) Eiffel days, when I was applying Design by Contract. Anyway, I can’t live without Debug.Assert. Imagine my dismay when I upgraded my Windows Phone 7.x app (Translator By Moth) to Windows Phone 8 and discovered that my Debug.Assert statements would not display anything on the target and would not break in the debugger any longer! Luckily, the solution was simple and in this post I share it with you – feel free to teak it to meet your needs.

Steps to use

  1. Add a new code file to your project, delete all its contents, and paste in the code from MyDebug.cs
  2. Perform a global search in your solution replacing Debug.Assert with MyDebug.Assert
  3. Build solution and test

Now, I do not know why this functionality was broken, but I do know that it exhibits the same broken characteristics for Windows Store apps. There is a simple workaround there to use Contract.Assert which does display a message and offers an option to break in the debugger (although it doesn’t output the message to the Output window). Because I plan on code sharing between Phone and Windows 8 projects, I prefer to have the conditional compilation centralized, so I added the Contract.Assert workaround directly in MyDebug class, so that you can use this class for both platforms – enjoy and enhance!