views:

150

answers:

2

Why does the below code not compile (snippet) ?

  public enum ApplicationType : int
  {
   CONSOLE = 1,
   WINDOWS_FORMS = 2,
   ASP_NET = 3,
   WINDOWS_SERVICE = 4,
   MUTE = 5
  }

        //#if( false)
        //#if (DEBUG && !VC_V7)
 #if( m_iApplicationType != ApplicationType.ASP_NET  )
        public class HttpContext
  {
   public class Current
   {
    public class Response
    {
     public static void Write(ref string str)
     {
      Console.WriteLine(str);
     }
    }
   }
  }
#endif
+5  A: 

What error are you getting?

In any case, ( m_iApplicationType == ApplicationType.ASP_NET ) is not a compile time constant.

Daniel Daranas
But it works with VB.NET
Quandary
@Quandary, C# is not VB.NET. And at least as far as http://msdn.microsoft.com/en-us/library/tx6yas69%28v=VS.90%29.aspx is concerned, it doesn't, nor does a worked example that I just tried.
Rob
+4  A: 

Your use of #if with a member variable is invalid. It operates only on symbols that you create with the #define directive, like so:

#define ASP_NET

#if(ASP_NET)
// put your conditional compilation code here
#endif

#if(CONSOLE)
// your console-related code goes here
#endif

In this case, only the code within the #if(ASP_NET) block will be compiled because CONSOLE is not defined.

Pre-compiler symbols (they don't get values, let alone values that vary so not really variables) can also be set on the compiler command line (including via MSBuild script or VS Project settings).
Richard