473,473 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Conditional Attribute on Method when the condition is met

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
Jun 27 '08 #1
7 2390
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

}
}
}
----------------------------
On Jun 4, 7:15*am, Frank Munsberg <bufferunder...@gmx.dewrote:
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
Jun 27 '08 #2
On Jun 4, 8:15*am, Frank Munsberg <bufferunder...@gmx.dewrote:
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
Jun 27 '08 #3
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.
On 4 Jun, 13:15, Frank Munsberg <bufferunder...@gmx.dewrote:
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
Jun 27 '08 #4
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/...-wpf-apps.aspx

http://www.hanselman.com/blog/Making...ateItself.aspx

http://www.codeproject.com/KB/vb/Cus...utoUpdate.aspx

http://episteme.arstechnica.com/eve/...m/319006601931

http://www.simple-talk.com/dotnet/.n...les-with-.net/

http://www.codeplex.com/sharpbits/Th...ThreadId=19428

http://www.codeproject.com/KB/cs/Man...select=1954607

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

On 4 Jun, 16:48, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
On Jun 4, 8:15 am, Frank Munsberg <bufferunder...@gmx.dewrote:
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
Jun 27 '08 #5
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?
Jun 27 '08 #6
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.
On 5 Jun, 08:46, Frank Munsberg <bufferunder...@gmx.dewrote:
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?
Jun 27 '08 #7
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.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chris han | last post by:
Hi, all, I'm trying to use Conditional Compilation Statements in my code. using System; #define DEBUG public class MyClass { public static void Main() {
2
by: FireStarter | last post by:
Guys, in the code that follows, why does the method F() still compile, even if DBG is undefined? Inside method G(), the code inside <#if DBG> does not compile (notice that I can write whatever I...
10
by: John Smith | last post by:
After reading C# documentation the Conditional attribute seemed the way to go, but after inspecting the IL it seems those methods are still there and I imagine the CLR removes them. Using #if DEBUG...
2
by: GlennDoten | last post by:
Upgraded to .NET 2.0 and VS.NET'05 and while compiling code that was fine under 1.1/2003 I'm getting a fatal compiler error: Conditional member 'GetTraceMode(out System.Web.TraceMode)' cannot...
6
by: Chris Dunaway | last post by:
Consider this code (.Net 2.0) which uses a nullable type: private void button1_Click(object sender, System.EventArgs e) { DateTime? nullableDate; nullableDate = (condition) ? null :...
0
by: GeorgeF | last post by:
How can I conditionally deploy shortcuts to my application? I have created a custom installer dialog box to give my users the option of creating desktop/start menu shortcuts, but the shortcuts do...
1
by: Marek | last post by:
I use VS2005 with framework 2.0 and I just found a behavior I consider odd. Here is the code that illustrates th eproblem: public static void MethodA() { MethodB() } #if DEBUG
2
by: Lyn | last post by:
Hi, Having fun trying to get Conditional Formatting working on a textbox control in a continuous form subform (Access 2003). The condition I want is when the value of the textbox is Null and/or...
3
by: aeon | last post by:
Working in Access 2007 on XP I have a combo box that I'm using in a form to select the WorkCategory assigned to a particular person in my database. WorkCategory is a lookup field in a table...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.