473,226 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Conditional compilation

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 want in there, I will not receive a
compilation error). I do get such an error in F() - because of the garbage I
intentionally put there - but F() should not compile in the first place.
Am I misusing this [Conditional...] attribute?!

#undef DBG
using System.Diagnostics;
class Class1 {

public Class1() {
int y = 0;
}

public void G() {

int x = 0;
x++;
#if DBG
// garbage here, that does not generate
// compilation errors - correct
x*&$%bbb
#endif
}

[Conditional("DBG")]
public void F() {
// garbage here, that generates compilation errors,
// although this is a conditional method, and DBG is undefined
x*&$%bbb
}
}

Nov 15 '05 #1
2 2552
Hi,

I think that it depend in which pass the different elements are parsed.

I'm not 100% sure of the next as my experience with the compilation process
is not that deep but it's valid in the "old" C world

the #undef DBG is a preprocessor directive , it's handle before the code
really reach a semantic or syntax parse, this is the first step to compile
the source code, this preprocesor get rid of the part that will not be
compiled therefore the compiler never sees the gargabe you write in this
part:
#if DBG
// garbage here, that does not generate
// compilation errors - correct
x*&$%bbb
#endif

Now if you use [Conditional("DBG")] this is not a preprocessor directive and
therefore it leave it there for the compiler to see, when it does reach the
compiler and it start doing the syntax parsing ( I think remember it's done
first than the semantic ) it see the wrong code and report it as error.

This must be more or less the process.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"FireStarter" <em***@server.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 want in there, I will not receive a
compilation error). I do get such an error in F() - because of the garbage I intentionally put there - but F() should not compile in the first place.
Am I misusing this [Conditional...] attribute?!

#undef DBG
using System.Diagnostics;
class Class1 {

public Class1() {
int y = 0;
}

public void G() {

int x = 0;
x++;
#if DBG
// garbage here, that does not generate
// compilation errors - correct
x*&$%bbb
#endif
}

[Conditional("DBG")]
public void F() {
// garbage here, that generates compilation errors,
// although this is a conditional method, and DBG is undefined
x*&$%bbb
}
}

Nov 15 '05 #2
Conditional code does get compiled with your project so it's got to be
valid. For instance, if you used two functions with the same name and
signature, marked one with Debug and the other with Release, you could
compile because there would be a conflict. My point being that it does get
compiled in.

As an aside, instead of using DBG which you set, why not use "DEBUG", then
when you switch the build mode to release, it's automatically handled
instead of you have to go into properties and include directives? Just a
suggestion.

HTH,

Bill
"FireStarter" <em***@server.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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 want in there, I will not receive a
compilation error). I do get such an error in F() - because of the garbage I intentionally put there - but F() should not compile in the first place.
Am I misusing this [Conditional...] attribute?!

#undef DBG
using System.Diagnostics;
class Class1 {

public Class1() {
int y = 0;
}

public void G() {

int x = 0;
x++;
#if DBG
// garbage here, that does not generate
// compilation errors - correct
x*&$%bbb
#endif
}

[Conditional("DBG")]
public void F() {
// garbage here, that generates compilation errors,
// although this is a conditional method, and DBG is undefined
x*&$%bbb
}
}

Nov 15 '05 #3

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

Similar topics

11
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be...
2
by: Steve Jorgensen | last post by:
To begin with an example... Let's say you were wanting to write code usign early binding to the MSXML library, but then be able to switch between early and late binding at will. Conditional...
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() {
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
1
by: A.M-SG | last post by:
Hi, We have a solution with several c# projects within it. How can I define solution wide conditional compilation symbols?
4
by: Bob | last post by:
Hi, In VS2003 conditional compilation constants and their state could be defined at project level. I was using this to control what features where offered by various builds. i.e....
10
by: Dave | last post by:
I'm a C++ programmer of many years, trying to get my feet wet in C#. I have a question about conditional compilation. In C++, I would sometimes define a constant in an include file, and then...
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
6
by: maxwell | last post by:
I'm trying to use the gpp utility (Gnu points to http://en.nothingisreal.com/wiki/GPP) to do conditional compilation in Python, and I'm running into a problem: the same '#' character introduces...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.