Debug Constant

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

This is going to be really stupid...but I often define method/properties
with an Attribute of Debug. When I compile for build, they cease to exist.
This is cool. However, is there a constant to the inverse? What I want to
do is if I have a Debug Build, I want to skip all sorts of log-on stuff and
the like. Right now, I'm manually setting a property in the build
properties, but I was wondering if there is a more elegant way to do it..ie
with Trace/Release or whatever else (I've tried these already).

Thanks,

Bill
 
William Ryan said:
This is going to be really stupid...but I often define method/properties
with an Attribute of Debug. When I compile for build, they cease to exist.
This is cool. However, is there a constant to the inverse? What I want to
do is if I have a Debug Build, I want to skip all sorts of log-on stuff and
the like. Right now, I'm manually setting a property in the build
properties, but I was wondering if there is a more elegant way to do it..ie
with Trace/Release or whatever else (I've tried these already).

There isn't an actual attribute called Debug as far as I'm aware -
however, there *is* the Conditional attribute, which can take any
string in its constructor as a symbol name, and if that symbol is
undefined the method/property won't be compiled (assuming the compiler
understands the attribute). You can just change your build settings to
define the symbol RELEASE, and use

[Conditional("RELEASE")]

in your code.
 
You can also use conditional compilation with defined symbols in the same
manner you would in C or C++. As in...

public void MyLoginMethod()
{
#if DEBUG
// some code here
#else
//other code here
#endif
}

One rule is that symbols defined in a source file must be before any other
C# statements. So put all the defines at the very top of the file, followed
by the using statements.

Jon Skeet said:
William Ryan said:
This is going to be really stupid...but I often define method/properties
with an Attribute of Debug. When I compile for build, they cease to exist.
This is cool. However, is there a constant to the inverse? What I want to
do is if I have a Debug Build, I want to skip all sorts of log-on stuff and
the like. Right now, I'm manually setting a property in the build
properties, but I was wondering if there is a more elegant way to do it..ie
with Trace/Release or whatever else (I've tried these already).

There isn't an actual attribute called Debug as far as I'm aware -
however, there *is* the Conditional attribute, which can take any
string in its constructor as a symbol name, and if that symbol is
undefined the method/property won't be compiled (assuming the compiler
understands the attribute). You can just change your build settings to
define the symbol RELEASE, and use

[Conditional("RELEASE")]

in your code.
 
Thanks both of you. I have used the # directives previsouly, but then I
saw there was a conditional debug attribute which worked cool, but I wanted
to have some code only execute for release builds. I tried using Release
but it didn't seem to work. Let me try it again though.

I can use the named directives, I was just wondering if there was a more
attribute focues way to do it.

Thanks again,

Bill
Dave said:
You can also use conditional compilation with defined symbols in the same
manner you would in C or C++. As in...

public void MyLoginMethod()
{
#if DEBUG
// some code here
#else
//other code here
#endif
}

One rule is that symbols defined in a source file must be before any other
C# statements. So put all the defines at the very top of the file, followed
by the using statements.

want
stuff
and
the like. Right now, I'm manually setting a property in the build
properties, but I was wondering if there is a more elegant way to do it..ie
with Trace/Release or whatever else (I've tried these already).

There isn't an actual attribute called Debug as far as I'm aware -
however, there *is* the Conditional attribute, which can take any
string in its constructor as a symbol name, and if that symbol is
undefined the method/property won't be compiled (assuming the compiler
understands the attribute). You can just change your build settings to
define the symbol RELEASE, and use

[Conditional("RELEASE")]

in your code.
 
Back
Top