471,610 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

"Conditional" attribute confusion

I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
Here is the code that illustrates th eproblem:

[Conditional("DEBUG")]
public static void MethodA()
{
MethodB()
}

#if DEBUG
public static void MethodB()
{
....
}
#endif

my expectation was that this code will compile perfectly when DEBUG
condition is defined as well as when it is undefined. Apparently, that is
not the case. When compiling with "DEBUG" undefined, compiler generates
error: "The name MethodB does not exist in current context."

This is what Microsoft help says about ConditionalAttribute class:
"Indicates to compilers that a method call or attribute should be ignored
unless a specified conditional compilation symbol is defined". The
conclusion from this is that MethodA from my example should not be compiled.
The reality is that the nethod is compiled - otherwise the error would not
have been reported.

Is there something I am missing about "Conditional" attribute and how the
compiler is working or it something .NET framework documentation or/and C#
compiler are missing ?

Thank you.
Nov 16 '06 #1
1 2128
Well, 2 minutes after I submitted this post I found the answer to my
question. Certainly it was me missing the point. "Conditional" attribute
informs compiler to ignore the call to the method but not to ignore the
method during the compilation.

My apology for posting something that I should not have.


>I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
Here is the code that illustrates th eproblem:

[Conditional("DEBUG")]
public static void MethodA()
{
MethodB()
}

#if DEBUG
public static void MethodB()
{
...
}
#endif

my expectation was that this code will compile perfectly when DEBUG
condition is defined as well as when it is undefined. Apparently, that is
not the case. When compiling with "DEBUG" undefined, compiler generates
error: "The name MethodB does not exist in current context."

This is what Microsoft help says about ConditionalAttribute class:
"Indicates to compilers that a method call or attribute should be ignored
unless a specified conditional compilation symbol is defined". The
conclusion from this is that MethodA from my example should not be
compiled. The reality is that the nethod is compiled - otherwise the
error would not have been reported.

Is there something I am missing about "Conditional" attribute and how the
compiler is working or it something .NET framework documentation or/and C#
compiler are missing ?

Thank you.


Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by delerious | last post: by
10 posts views Thread by John Smith | last post: by
3 posts views Thread by ARZ | last post: by
7 posts views Thread by What-A-Tool | last post: by
1 post views Thread by Christian Welzel | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by
reply views Thread by CCCYYYY | last post: by

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.