In a typical day, I write or debug programs in several languages: typically Foxpro, C#, VB, C++ and 32 bit assembly, with an occasional MSIL, IDL and 64 bit ASM thrown in.

 

Sometimes, I like to switch between one version of code and another. This is useful if I want to do side by side comparisons of behavior.

 

One way to do this is with preprocessor macros, like this:

 

#If  SomeValue

                <one version of code>

#else

                <another version>

#endif

 

However, that’s a fair amount of typing.

 

There’s a shortcut that works with C# and C++ style comments.

 

In these languages, a line that starts with “//” is a comment.

 

Also, a block comment (which can span multiple lines) starts with “/*” and ends with “*/”

 

 

 

//*

      int sub foo1() {

            int x = 2;

                  Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo1

            return x;

      }

 

/*/

      int sub foo2() {

            int x = 3;

                  Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo2

            return x;

      }

// */

 

With a single character change I can switch between foo1 and foo2: just delete the very first “/”.  That changes the single line comment into a block comment. The “*/” of the “/*/” now acts like the end of the comment block.

 

Using an editor that colors the code (like Visual Studio) shows the switch properly

 

 

 

/*

      int sub foo1() {

            int x = 2;

                  Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo1

            return x;

      }

 

/*/

      int sub foo2() {

            int x = 3;

                  Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo2

            return x;

      }

// */

 

This technique is useful when creating sample code for others to play with, such as in my next blog post.