Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2010, 03:38 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default How do you put an "IF DEBUG" condition in a c# program?

How do you put an "IF DEBUG" condition in a c# program?

How do you put an "IF DEBUG" condition in a c# program so that, at run time, it will ignore a set of code if you are running in Debug mode and yet, execute a block of code if the program is not running in debug mode? I mean, what does it look like? Can you give me an example of what this source code would look like?

A situation where this can be used is if a time stamp is taken at the start of a block and another time stamp is taken at the end. THey will hardly differ at run time. Yet, if you are stepping through the code in debug mode, they will differ a lot, and error conditions in an "if block" might be kicked off leading to the untimely (pun) execution of some code.

There must be other cases where the need for such a thing exists.

I have used this in C++ but I forgot what it was. I tried googling but I did not get any results at first glance.

Also, what would be an "IF NOT DEBUG" condition and the closing "END IF"?
 
Old July 10th, 2010, 01:11 AM
Registered User
 
Join Date: Jul 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
#if DEBUG
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\myDebugDatabase.mdb";
#else
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\myPublishDatabase.mdb";
#endif

OleDbConnection myCon = new OleDbConnection(conStr);

//Some code here
Hope this helps
 
Old August 31st, 2010, 03:26 AM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Post

it checks whether the DEBUG symbol exists in your code or not.
You can define these symbols using #define dirctive.
For best help I an exact bit of copy from the book professional C# 4 and .Net 4 :
Code:
#define ENTERPRISE
#define W2K

// further on in the file
#if ENTERPRISE
   // do something
   #if W2K
      // some code that is only relevant to enterprise
   // edition running on W2K
   #endif
#elif PROFESSIONAL
   // do something else
#else
   // code for the leaner version
#endif
Great wishes, Best Luck...
 
Old August 31st, 2010, 03:36 AM
Authorized User
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Post

Sorry For bugs of my previous post
I was so tired....

errata:

It checks whether the DEBUG symbol exists in your code or not.
You can define these symbols using #define directive.
For best help I copy an exact bit of code from the book "professional C# 4 and .Net 4" :
Code:
#define ENTERPRISE
#define W2K

// further on in the file
#if ENTERPRISE
   // do something
   #if W2K
      // some code that is only relevant to enterprise
   // edition running on W2K
   #endif
#elif PROFESSIONAL
   // do something else
#else
   // code for the leaner version
#endif
I do apologize again . . .





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties with "web.config" and "ASPNETDB" CFRham BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 2 July 3rd, 2010 10:19 AM
Need code corrections "get sub or function not defined" error" bluesboytoo Excel VBA 1 October 22nd, 2009 11:12 PM
How to theme the "Browse" button of "FileUpload" control? varunbwj BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 October 14th, 2009 01:22 AM
Add a CheckBox DataColumn to my DataGridView, Null format: "" or "True" but Error: F ismailc C# 2005 0 September 25th, 2009 04:56 AM
Code not going as planned: "icicle" vs "savedinstancestate" joopthecat BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 May 3rd, 2009 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.