Conditional Attribute on Method when the condition is met

F

Frank Munsberg

Hi There,

I'm currently in a mild state of confusion about the Conditional
Attribute. The code below is taken from the implementation of "Design
By Contract" that I've put in somewhere in my project. As you might
expect the code outputs "DBC_CHECK_ALL is defined" when
Check.Require(...) is called and the project is compiled with DEBUG
settings.

#define DBC_CHECK_ALL

public public sealed class Check
{
[Conditional("DEBUG")]
public static void Require(bool assertion, string message)
{
#if DBC_CHECK_ALL
Console.WriteLine("DBC_CHECK_ALL is defined");
#endif
//...<some code that needs to be executed>...
}
//...<some other functions>...
}

Now if I replace the [Conditional("DEBUG")] with
[Conditional("DBC_CHECK_ALL")] the whole method isn't executed
anymore. Can anyone explain to me why this is happening or what on
earth I'm missing here?
I suspect its something blatantly obvious but I can't find it for the
life of me.

Greetings
Frank
 
Q

qglyirnyfgfo

It works for me, below is the exact code that I used after crating a
brand new Console application. If I am missing the point, it may help
if you post a small but complete working example that reproduces your
problem.

----------------------------
#define DBC_CHECK_ALL

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication326
{
class Program
{
static void Main(string[] args)
{
Check.Require();
Console.Read();
}
}

public sealed class Check
{
[Conditional("DBC_CHECK_ALL")]
public static void Require()
{
Console.WriteLine("Inside Require");

#if DBC_CHECK_ALL
Console.WriteLine("DBC_CHECK_ALL is defined");
#endif

}
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi There,

I'm currently in a mild state of confusion about the Conditional
Attribute. The code below is taken from the implementation of "Design
By Contract" that I've put in somewhere in my project. As you might
expect the code outputs "DBC_CHECK_ALL is defined" when
Check.Require(...) is called and the project is compiled with DEBUG
settings.

#define DBC_CHECK_ALL

public public sealed class Check
{
[Conditional("DEBUG")]
public static void Require(bool assertion, string message)
{
        #if DBC_CHECK_ALL
        Console.WriteLine("DBC_CHECK_ALL is defined");
        #endif
//...<some code that needs to be executed>...}

//...<some other functions>...

}

Now if I replace the [Conditional("DEBUG")] with
[Conditional("DBC_CHECK_ALL")] the whole method isn't executed
anymore. Can anyone explain to me why this is happening or what on
earth I'm missing here?
I suspect its something blatantly obvious but I can't find it for the
life of me.

Greetings
Frank

Work here

Just note that if you declare your #define in a file it ONLY exist in
that particular file
 
S

ssg31415926

It's gotta be a typo!. I created a new console project in VS2005 and
pasted in your code:

#define DBC_CHECK_ALL

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ConditionalAttributes
{
public sealed class Program
{
static void Main(string[] args)
{
Require(true, "fred");

Console.WriteLine("\r\nPress a key to continue...");
Console.ReadKey(true);

}

//[Conditional("DEBUG")]
[Conditional("DBC_CHECK_ALL")]
public static void Require(bool assertion, string message)
{
#if DBC_CHECK_ALL
Console.WriteLine("DBC_CHECK_ALL is defined");
#endif
//...<some code that needs to be executed>...
}

//...<some other functions>...
}
}

It works fine with either ConditionalAttribute defined, as you'd
expect but not as you saw.
 
S

ssg31415926

If anyone else is looking, here are a bunch of references I found.
The first two are definitely worth a look:

http://www.devx.com/dotnet/Article/10045/0/page/1

http://msdn.microsoft.com/en-gb/magazine/cc188766.aspx

http://blogs.msdn.com/dotnetinterop/archive/2008/03/28/simple-auto-update-for-wpf-apps.aspx

http://www.hanselman.com/blog/MakingYourApplicationAutomaticallyUpdateItself.aspx

http://www.codeproject.com/KB/vb/CustomAppAutoUpdate.aspx

http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/319006601931

http://www.simple-talk.com/dotnet/.net-tools/using-bits-to-upload-files-with-.net/

http://www.codeplex.com/sharpbits/Thread/View.aspx?ThreadId=19428

http://www.codeproject.com/KB/cs/Managed_BITS.aspx?df=100&forumid=337909&exp=0&select=1954607

http://www.codeproject.com/KB/IP/sharpBITS.aspx

Hi There,
I'm currently in a mild state of confusion about the Conditional
Attribute. The code below is taken from the implementation of "Design
By Contract" that I've put in somewhere in my project. As you might
expect the code outputs "DBC_CHECK_ALL is defined" when
Check.Require(...) is called and the project is compiled with DEBUG
settings.
#define DBC_CHECK_ALL
public public sealed class Check
{
[Conditional("DEBUG")]
public static void Require(bool assertion, string message)
{
#if DBC_CHECK_ALL
Console.WriteLine("DBC_CHECK_ALL is defined");
#endif
//...<some code that needs to be executed>...}
//...<some other functions>...

Now if I replace the [Conditional("DEBUG")] with
[Conditional("DBC_CHECK_ALL")] the whole method isn't executed
anymore. Can anyone explain to me why this is happening or what on
earth I'm missing here?
I suspect its something blatantly obvious but I can't find it for the
life of me.
Greetings
Frank

Work here

Just note that if you declare your #define in a file it ONLY exist in
that particular file
 
F

Frank Munsberg

I hope this doesn't become a double posting.

anyway, I just did the same as everybody else and moved the function
with the conditionals into a small console app. That worked like a
charm so things are not totally broken.
Then I made it a class library to call the Require function via an
NUnit Test function (2.4.6) because the real project is run by NUnit
too at this point.

When run from NUnit the results are the same as in the real project so
somehow NUnit is messing with either the #define at the top of the cs
file or with the Conditional. It works when I use the
Conditinoal("DEBUG") but not the DBC_CHECK_ALL. At least I've tracked
the culprit down but I still don't know why this is happening. Does
anyone have an idea?
 
S

ssg31415926

Sorry. Had umpteen tabs open in Firefox and clicked reply in the
wrong one - was wondering what had happend to my reply to the other
thread.

Have you tried the NUnit mailing list - the guys there can be really
helpful.
 
F

Frank Munsberg

Heh, window madness :)
I'll try posting something there. If nothing helps then I'll use some
workaround.
enclosing the code inside the function(s) with #if DBC_CHECK_ALL <...>
#endif or something like that should also do the trick. Even if its
messy its better than nothing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top