473,385 Members | 1,342 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,385 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 16517
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.