Reputation: 16724
For example:
define.cs
#define FOO
form1.cs
#if FOO
MessageBox.Show("foo is set!");
#else
MessageBox.Show("foo is not set!");
#endif
the define.cs are included on same projet that form1.cs but the above condition give: foo is not!
Upvotes: 9
Views: 9819
Reputation: 3321
In C#, scope of #define is only limited to file in which it is declared.
To define a symbol visible in all files, one way is to Project->Properties->Build->Conditional Compilation Symbols and specify the symbol "FOO" there.
Upvotes: 6
Reputation: 6850
As others have said, a #define is scoped to a single file.
However, you can define preprocessor variables that are scoped to the entire assembly. To do this you just have to go into the "Build" section of the project's settings and put it in the "Conditional compilation symbols" section. (This is just an easier way to manage the /define compiler switch.) That approach has the added advantage of also letting you define different sets of symbols for different build configurations.
Upvotes: 1
Reputation: 31394
You can't, but what you can do is move the define to the project configuration so that all files in the project can see the definition.
See the instructions in How to define a constant globally in C# (like DEBUG).
Upvotes: 14
Reputation: 46559
According to this MSDN page,
The scope of a symbol created with #define is the file in which it was defined.
Upvotes: 3
Reputation: 6490
You Can't, The scope of a symbol created with #define is the file in which it was defined.
Upvotes: 1