How to use #define from another file?

…衆ロ難τιáo~ 提交于 2019-12-23 08:58:21

问题


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!


回答1:


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).




回答2:


According to this MSDN page,

The scope of a symbol created with #define is the file in which it was defined.




回答3:


You Can't, The scope of a symbol created with #define is the file in which it was defined.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/10916280/how-to-use-define-from-another-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!