views:

469

answers:

3

Hello, I am having trouble getting Visual Studio to behave as I would expect it. I created 2 configuration profiles. One has the symbol FOO defined and the other has the symbol BAR defined. And I have this code:

static class MyClass{
#if FOO
  public static string const MyData="foo defined";
#endif
#if BAR /*yes, I know #elif would work here too.. just trying to be simple*/
  public static string const MyData="bar defined";
#endif
}

and then later in another file I have

 if(MyClass.MyData=="foo defined").....

Well, in my application, I get an error that MyClass.MyData is not defined.

Also, if I have it on the FOO profile and type in something like #error test after the #if FOO then it will have a build error, but if I remove that it will build just fine and when I go to run it I'll get a compilation error that MyClass does not contain a definition for MyData. Also, this is an ASP.Net Web Application.

Can someone help me figure out how to use conditional compilation? It acts as if Visual Studio compiles it properly with the compilation symbols, but whenever the ASP.Net webserver executes it, it recompiles it without any symbols... But that just doesn't make any sense why it would do that..

Edit: It doesn't matter if I use the FOO or BAR profile, neither of them seem to define the MyData symbol as they should.

EDIT2:

Ok, this is important for reproducing!!! Create a new App_Code folder in your solution and then add a .cs file there and then add MyClass to it. This will reproduce the bug working in a blank project. I've actually simplified it down to just

#if !(FOO || BAR)
  #error neither foo or bar defined
#endif

It seems like Visual Studio does not set conditional compile symbols for regular .cs files inside of App_Code

+2  A: 

Since ASP.NET compiles your code outside of your development environment, it uses its own build configuration. You must set the conditional compilation symbol in your web.config, see link here: Conditional Compilation in ASP.NET 2.0

Aviad P.
Thats what I did. In the FOO configuration profile, I added the symbol FOO and for the BAR profile I added the symbol BAR
Earlz
Weird, the problem must lie somewhere else, are you sure you're actually building using the correct configuration?
Aviad P.
yes.. Like I said, it builds without throwing an error, but then when I run the asp.net website it will load up a Compilation Error page
Earlz
Change the Configuration from Debug to Release. Set the symbol there as well.
Hans Passant
Take a look now - this was your problem probably.
Aviad P.
Possibly.. so you mean if I have my web.config checked into source control, then I must modify my web.config for different configurations(which we test between very often)? That is a crappy solution. Why does it work in my say default.aspx.cs file, but not in app_code/test.cs?
Earlz
ok ok... apparently `app_code` is treated as a special folder.... If I rename my app_code folder to `app_code_cs` then it all works as expected... Silly microsoft doesn't tell you about that.. I'm giving you the answer because well.. it's what kicked me and made me figure it out..
Earlz
A: 

If you are trying to check for the definition of a pre-processor directive and not the replacement token's actual value, then you should try the following:

#if defined(FOO)
  public static string const MyData="foo defined";
#elif defined(BAR)
  public static string const MyData="bar defined";
#else
#error neither foo nor bar defined
#endif

This will evaluate based on whether or not FOO or BAR are defined. The version posted in the question was actually evaluating based on FOO's and BAR's replacement token values.

Dennis
`defined` does not appear to work for me.. I just get a syntax error for it.
Earlz
Does #ifdef work? For example '#ifdef FOO'
Dennis
I believe you are confusing C and C#...
Earlz
My answer was for a C++ pre-processor - not c#. My mistake.
Dennis
A: 

I'm having a hard time reproducing your problem. I do have a very simple web app that uses conditional compile. I would be happy to post the solution somewhere. I don't see an attachment option here. I could send you a zip file, or if you know of a place where I could post the zip file, I could put it there (do you have a shared skydrive or anything?)

JMarsch
try http://www.4filehosting.com/
Earlz
I'm afraid 4filehosting didn't work for me -- I tried the "form" based method (errored out), and the ftp-based method (hung). I guess you get what you pay for <g> If there is any other way you would like for me to get this file to you, I'd be happy to oblige.
JMarsch