By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Conditional Compilation/Conditional Constants

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.