Conditional Compilation/Conditional Constants

  • Thread starter Thread starter chris han
  • Start date Start date
C

chris han

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
 
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.
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
 
Back
Top