0

I have a config file that contains all the configurations of my program. In that file, I define a directive as follow:

---------- MyConfig.cs ------------

#define TEST_LOCALHOST

public class MyConfig
{
   ....
}

Now, from another file (MyWebService.cs), I define a variable MyServerName based on the directive TEST_LOCALHOST is defined or not as follow:

---------- MyWebService.cs ------------

using MyData;

public class MyWebService
{

#if (TEST_LOCALHOST)
    private static string MyServerName = "http://localhost:8888";
#else
    private static string MyServerName = "http://MYSERVER";
#endif

}

The namespace structure in my program is:

| ----- WebService
| ---------------- MyWebService.cs
|
| ----- Data
| ---------------- MyConfig.cs

However, the TEST_LOCALHOST is always not defined in MyWebService.cs. I mean, my variable MyServerName is always pointing to "http://MYSERVER", but not to "http://localhost".

Am I missing something?

Bernd Linde
  • 2,098
  • 2
  • 16
  • 22
chipbk10
  • 5,783
  • 12
  • 49
  • 85

2 Answers2

3

As per the MSDN documentation,

The scope of a symbol that was created by using #define is the file in which the symbol was defined.

Because of this, it will not be beneficial to use a #define directive.
Rather use a property created in your web.config file and change the value of MyServerName according to that property

Bernd Linde
  • 2,098
  • 2
  • 16
  • 22
  • Bernd Linde gave a good answer. As he (almost) said, your case looks much more an application setting : I think it should not be handled as a compilation symbol! – AFract Feb 09 '15 at 09:45
  • I actually agree, this looks more like what the original user intended to do. But I just thought that since this code intended was to load testing strings, he didn't want to keep these 'symbols' visible in production code. Anyhow, that's the way to go. – DarkUrse Feb 09 '15 at 09:53
3

To make sure it is located everywhere, I would either define this pre-processor switch in the project's settings itself (In Visual Studio, go to "Build" tab from the project's properties page and define the switch in the "Conditional Compilation Symbols" section); either define it the compiler switch on the command line with '/define:name1[;name2]'

DarkUrse
  • 2,084
  • 3
  • 25
  • 33