Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a constant globally in C# (like DEBUG)

Tags:

c#

.net

I want to compile a project differently, according to a constant defined by #define, like this:

#define USE_COMPONENT_X

#if USE_COMPONENT_X
...

#endif

and I can do that in C#. But when I go to another file in the same project, this constant is not defined. Can I in some way define a constant to all the project, like DEBUG is defined so?

like image 574
Victor Rodrigues Avatar asked Jan 12 '09 18:01

Victor Rodrigues


People also ask

How do you define a global constant?

Global Constants. A global constant is a literal value to which you assign a name. Like a global variable, you can access the value of the global constant from any script or 4GL procedure in the application. You set the value for the global constant when you declare it.

How do you make a global const?

A constant which is needed in more than one functions can be declared a global constant by declaring it a constant using the reserve word const, initializing it and placing it outside of the body of all the functions, including the main function.

How do you declare constants in C?

The const keyword Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.


2 Answers

You can add the /define compiler switch.

  1. Open the project's Property Pages dialog box.
  2. Click the Configuration Properties folder.
  3. Click the Build property page.
  4. Modify the Conditional Compilation Constants property.
like image 179
Jason Diller Avatar answered Sep 19 '22 07:09

Jason Diller


You may want to go a step further and create different project configurations as variants of the standard Debug and Release project configuration. The Configuration Manager under the build menu will let you accomplish this. Then while you are in the project properties' Build tab you can select the various configurations and set the conditional compilation constants that are appropriate for each configuration. This will save you lots of time when you want to swap back and forth between various permutations of your conditionally compiled code.

like image 29
EnocNRoll - AnandaGopal Pardue Avatar answered Sep 23 '22 07:09

EnocNRoll - AnandaGopal Pardue