Company      |       Articles & Research      |      Services      |      Software      |      Contact

Neat Commenting Trick With #Debug

I wish I would have known about this trick a long time ago. Jim Sally whom I work with demonstrated to me a really neat commenting trick that I did not know about, that really helps when you are mocking out objects particularly. When you have some arbitrary class file, like this jawdropper :)

C#:
  1. using System;
  2. using Microsoft.SharePoint;
  3.  
  4. namespace examples.SharePoint.Shelter
  5. {
  6. public class List
  7. {
  8. private readonly SPWeb m_oWeb;
  9.  
  10. public List()
  11. {
  12. m_oWeb = null;
  13. }
  14.  
  15. public SPWeb WebSite
  16. {
  17. get { return m_oWeb; }
  18. }
  19.  
  20. public string GetListDirection(Guid oListId)
  21. {
  22. SPList l_oList = m_oWeb.Lists[oListId];
  23. return l_oList.Direction;
  24. }
  25.  
  26. }
  27. }

There is just that string return that results in the reading direction of an arbitrary SharePoint list object, but what if you wanted to comment out that method, however because you use it for and in development, you want it AUTOMATICALLY commented out when you switch between debug (development) and release (production) build configurations, selected from the Visual Studio IDE. Jim showed me you can just wrap whatever you want with this:

C#:
  1. #if (DEBUG)
  2.  
  3. #endif

So that the class file would instead look like this:

C#:
  1. using System;
  2. using Microsoft.SharePoint;
  3.  
  4. namespace examples.SharePoint.Shelter
  5. {
  6. public class List
  7. {
  8. private readonly SPWeb m_oWeb;
  9.  
  10. public List()
  11. {
  12. m_oWeb = null;
  13. }
  14.  
  15. public SPWeb WebSite
  16. {
  17. get { return m_oWeb; }
  18. }
  19. #if (DEBUG)
  20. public string GetListDirection(Guid oListId)
  21. {
  22. SPList l_oList = m_oWeb.Lists[oListId];
  23. return l_oList.Direction;
  24. }
  25.  
  26. #endif
  27.  
  28. }
  29. }

And when you switch the build type configuration, it is automagically commented out. You can define your own, or use the ones that Visual Studio provides for you.

share save 171 16 Neat Commenting Trick With #Debug

Related posts:

  1. Return A Document Library GUID In C#
  2. Get SPList By URL In C#
  3. Controlling SPField Display
  4. SharePoint Security Helper Class
  5. Get Locale From SPWeb URL

2 Comments »

  1. [...] I wish I would have known about this trick a long time ago. Jim May whom I work with demonstrated to me a really neat commenting trick that I did not know about, that really helps when you are mocking out objects particularly. When you have some arbitrary Read More……(read more) September 26th 2007 Posted to Uncategorized [...]

    Pingback by Blogger Loser » Neat Commenting Trick With #Debug — September 26, 2007 @ 8:54 am

  2. Try using this site for formatting your code…

    http://formatmysourcecode.blogspot.com/

    Comment by Jim Sally — October 4, 2007 @ 7:11 am

RSS feed for comments on this post. TrackBack URL

Leave a comment