473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Conditional Compilation/Conditional Constants

Hi, all,

I'm trying to use Conditional Compilation Statements in my code.

using System;
#define DEBUG
public class MyClass
{
public static void Main()
{

#if (DEBUG)
Console.WriteLine("DEBUG is defined");
#else
Console.WriteLine("DEBUG is not defined");
#endif
}
}

What I'm going to achieve is instead of using Conditional Constants #define DEBUG, can I get the Condition from the Visual studio's configuration manager? So that when I compile for Debug or Release, I can get different for the code above?

TIA
Chris

Nov 13 '05 #1
1 16510
Hello Chris,

That is exactly what conditional complier statements are for. When your configuration is set in DEBUG, by default, the VS.NET IDE automatically sets the Conditional Compiler Constants to be "DEBUG;TRACE" (You can find out what constants are defined by going to your Project properties and selecting Configuration Properties>Build) and when set in RELEASE, it only sets the constants to be "TRACE".

What I normally do is add "RELEASE" to my RELEASE configuration and remove "TRACE". Then I condition my code as follows:

#define DEBUG
#define TRACE
#define RELEASE
using System;

public class MyClass
{
public MyClass()
{
#if (DEBUG)
Debug.WriteLine("In debug mode...");
#else if (TRACE)
Debug.WriteLine("In trace mode...");
#else if (RELEASE)
//In release.. nothing is outputted
#endif
}
}
(Just for reference)
This is what my .csproj file looks like for my "default" configs:
<Config
Name = "Debug"
AllowUnsafeBlocks = "true"
BaseAddress = "285212672"
CheckForOverflowUnderflow = "false"
ConfigurationOverrideFile = ""
DefineConstants = "DEBUG"
DocumentationFile = "bin\Debug\debug_docs.xml"
DebugSymbols = "true"
FileAlignment = "4096"
IncrementalBuild = "true"
Optimize = "false"
OutputPath = "bin\Debug\"
RegisterForComInterop = "true"
RemoveIntegerChecks = "false"
TreatWarningsAsErrors = "false"
WarningLevel = "4"
/>
<Config
Name = "Release"
AllowUnsafeBlocks = "false"
BaseAddress = "285212672"
CheckForOverflowUnderflow = "true"
ConfigurationOverrideFile = ""
DefineConstants = "RELEASE"
DocumentationFile = "bin\Release\release_docs.xml"
DebugSymbols = "false"
FileAlignment = "4096"
IncrementalBuild = "false"
Optimize = "true"
OutputPath = "bin\Release\"
RegisterForComInterop = "true"
RemoveIntegerChecks = "false"
TreatWarningsAsErrors = "false"
WarningLevel = "0"
/>
<Config
Name = "Trace"
AllowUnsafeBlocks = "true"
BaseAddress = "285212672"
CheckForOverflowUnderflow = "true"
ConfigurationOverrideFile = ""
DefineConstants = "TRACE"
DocumentationFile = "bin\Trace\trace_docs.xml"
DebugSymbols = "false"
FileAlignment = "4096"
IncrementalBuild = "false"
Optimize = "true"
OutputPath = "bin\Trace\"
RegisterForComInterop = "true"
RemoveIntegerChecks = "false"
TreatWarningsAsErrors = "false"
WarningLevel = "0"
/>

Also, make sure that your #define statements are the first lines in your code, otherwise the compiler will complain.. ;)

HTH,

Bill P.
"chris han" <ch*******@baxter.com> wrote in message news:u%***************@tk2msftngp13.phx.gbl...
Hi, all,

I'm trying to use Conditional Compilation Statements in my code.

using System;
#define DEBUG
public class MyClass
{
public static void Main()
{

#if (DEBUG)
Console.WriteLine("DEBUG is defined");
#else
Console.WriteLine("DEBUG is not defined");
#endif
}
}

What I'm going to achieve is instead of using Conditional Constants #define DEBUG, can I get the Condition from the Visual studio's configuration manager? So that when I compile for Debug or Release, I can get different for the code above?

TIA
Chris
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Victor Irzak | last post by:
Hello, I want to build two assemblies that use common c# code. I wish to use preprocessor. I defined a constant YABA in "Conditional compilation constants" in the property pages. So the line...
13
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
2
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I...
7
by: John Dolinka | last post by:
I have a project of several files with #defines to setup a conditional compile. I would like to put these #defines in a single file and change the conditional compile from this one file with the...
5
by: Trapulo | last post by:
I'm using conditonal compile with statement #IF CONFIG This works if I use #IF CONFIG = "Debug" or #IF CONFIG= "Release" But if I define an other compilation profile, eg. "BestRelease", and I use...
4
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e....
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
1
by: Sagaert Johan | last post by:
Hi Is there a constant i could use to force conditional compilation based on whether i compile for CF or the Full NET framework ? Johan
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.