468,234 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,234 developers. It's quick & easy.

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 16141
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.

Similar topics

reply views Thread by Victor Irzak | last post: by
12 posts views Thread by wanghz | last post: by
2 posts views Thread by FireStarter | last post: by
5 posts views Thread by Trapulo | last post: by
4 posts views Thread by Bob | last post: by
10 posts views Thread by Dave | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.