Search
Tuesday, October 16, 2018 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

You ever need to a piece of code, BUT just for debugging?

Apr 30

Written by:
Thursday, April 30, 2009 7:45 PM  RssIcon

Have you ever wanted to write a piece of code JUUUUUUUST for debugging purposes?  Ever been burnt with leaving it in for a Release build?  Read on for a really quick'n' easy way to avoid this in the future!

Come on now!  Admit it! We've all done it! 

We've all put in a printf() statement, or Console.WriteLine() or something to that affect to "just peek at a variable quickly!  I'll pull it out right after I'm done!"  Next thing you know, you're going down that rabbit hole so far, you've lost track of where you can from, 10-15 Console.WriteLine's later, you've found 14 of them!  DOH!  You missed one!  How did you find that out?  Cause a customer is calling (or worse, your boss) about why they're seeing a certain piece of cryptic text. 

A bit embarassing?  Strike a little too close to home?  Check out this quick'n'dirty little precompiler command to help save yoursef from that in the future!  Use the #if DEBUG precompile statement to enter stuff JUUUUST for the DEBUG build.  It's not compiled in for RELEASE builds.

#if DEBUG               //sets up VS for debugging, when you see the dlg popup then you can pick your VS or a new one to debug with
      System.Diagnostics.Debugger.Break();
#endif

You could use the Debugger.Launch() if you're developing something like a Windows Service where you're running the application outside of Visual Studio but want it to hook up with a debugging instance of VS.  But if you're doing regular WinForms/WPF apps, the Debugger.Break() should work just great for you.

I hope this little tidbit will help save you a bit of embarassment in the future.  Now go grab a coffee and get coding! :>

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement