Thursday 23 October 2008

Triggering code depending on build type


In C#.NET we have 2 common build modes: Debug and Release. We use Debug to test the code (I.e. insert breakpoints, step through code) which generates our .PDB files (These contain debug symbols used by the compiler. However, in Release mode, we do not have these debug symbols as when we deliver a product it its user, they have nothing to do with the way the code is tested.

If we have two sets of data, I.e. two databases... one for our test environment and one for our live environment, we can make life easier by using the following code snippet to establish which database to use, depending upon the build type!

#if RELEASE
// Use release database here
#else
// Use test database here
#endif


(RELEASE can also be switched to DEBUG for the reverse effect).

When the build method is changed within VS.NET, the relevant code (for its build type) is highlighted and the code un-used is grayed out.

No comments: