21

Sorry if my terminology is wrong. I wrote #if TEST_APP in my code. Now i would like to define TEST_APP. How do i set it using visual studios 2010? This is a windows form application.

Bonus if you can tell me the name of the symbol that is set in a winform project and in a web project

H H
  • 263,252
  • 30
  • 330
  • 514

4 Answers4

35

In visual studio solution explorer, right click on a project and click Properties. Open the build tab and you will see a field "Conditional compilation symbols". This is a comma separated list, or space separated. There are also 2 checkboxes for commonly used symbols, DEBUG and TRACE.

For your web projects you could set the field to "WEB_PROJECT" and winforms to "WINFORMS_PROJECT"

Ramon Zarazua B.
  • 7,195
  • 4
  • 22
  • 26
Russell
  • 17,481
  • 23
  • 81
  • 125
  • 4
    You can also set these things directly in your project file(*.csproj) for example `DEBUG;TRACE` inside a PropertyGroup. – Jeyenne Jul 21 '16 at 23:39
8

In the Build tab of the properties page for the project, look for the "Conditional compilation symbols" setting.

I don't believe there are any different symbols defined by default for web and winform applications. Bear in mind this is set for the project itself, and won't affect any class libraries - so I'd expect any code within a project to really know whether it's in a Windows application or not to start with. What were you thinking of using this for?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • after writing APP_TEST -dAPP_TEST (i am thinking gcc) and them fail and after seeing these answers i realize i had a reference to my web lib instead of including my files. oops. That was when i tried getting F# and C# to define methods in the same partial class. –  Mar 01 '10 at 11:24
  • I dont need the app and web define, i just wondered since i know C++ does it. I am testing some backend code in an app so i can modify it on the fly which i cant do in a web application (Changes are not allowed in the following case. Debugger attached to an already running process) –  Mar 01 '10 at 11:28
  • We used this in a webapplication to enable caching in debug. In Release caching logic was enabled, in debug disabled but sometimes you want to test caching logic (in debug). For this is an extra conditional is handy. (DEBUG | DEBUG_WITH_CACHE) – bob Mar 01 '10 at 11:32
0

Method 1:

#define TEST_APP true
#if TEST_APP == true
#endif

Method 2:

#define TEST_APP
#if defined(TEST_APP)
#endif

Source: MSDN

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • Are you sure you are talking about C#, not C++? According to this https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives, C# does not support values for defined symbols. – C-F Jan 10 '22 at 23:48
0

If you need your conditional compilation to dynamically reflect the build or environment conditions, check my answer to How do I set a conditional compile variable on StackOverflow. I show how to enable conditional compilation based on ambient conditions, such as C# language version, so that you can write code like this:

#if CSHARP7
    ref T pi = ref rg[i], pj = ref rg[j];
    var t = pi;                    // swap elements: managed pointers
    pi = pj;
    pj = t;
#else
    var t = rg[i];                 // swap elements: clunky
    rg[i] = rg[j];
    rg[j] = t;
#endif
Community
  • 1
  • 1
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108