1

I have a website project I inherited and am attempting to clean up. It had a Common.cs file with over 10,000 loc so I have since separated it out into different files. The problem is that at the top of the Common file is a #define UAT statement that is used throughout the code to make certain configuration decisions such as :

#if UAT
    using WCFServiceUAT;
#else
    using WCFServicePRD;
#endif

So now, when I go to deploy the production version of this application, I will have to remove the #define statement in many different places, which seems error prone and a generally bad idea. I am looking for something such as Conditional Compilation Constants where I can define this once, and have it affect the entire project.

This type of configuration control is only being used in C# files. the #define statement used to only need to be changed in Default.aspx.cs and Common.cs, but since my restructuring efforts, it now appears a lot more. Though it would be nice for my site.master file to change the header based off of some configuration, it is much less of a concern.

I have attempted to alter the build configuration properties for the project but do not have any options such as conditional compilation constants and am assuming that it is not supported for my type of project. Is there any other way to put a #define on a global project level, instead of at the top of every file? The only solution I have found is for a Web application project, and based on Web Application Projects Vs Web Site Projects, I do not believe I am working with a web application type project because there is no .csproj file.

Cole9350
  • 5,444
  • 2
  • 34
  • 50
  • 1
    Selecting the properties of your project, in the second page (BUILD) you should see the inputbox "Conditional compilation Symbols" where you could set/remove your UAT symbol – Steve Jul 09 '15 at 17:52
  • I am saying it is not there though. I have `Before running startup page:` , `Target Framework` , `Build solution action` and `Access validation` – Cole9350 Jul 09 '15 at 17:55
  • I see, it is a web-site project then and this kind of project seems to be missing of this 'conditional symbols'. Well, it seems that the only option is to convert your project to a Web Application. [Here a step-through](http://stackoverflow.com/questions/735054/how-to-convert-asp-net-website-to-asp-net-web-application) – Steve Jul 09 '15 at 18:13
  • Ouch... I was honestly afraid that would be the only answer – Cole9350 Jul 09 '15 at 18:22
  • I am not an expert on this subject so you could wait to see if someone has a better answer for you – Steve Jul 09 '15 at 18:31
  • @Steve I tried the link you attached but now am receiving "the Type or namespace name 'WCFServiceUAT' could not be found. The only way I can get the above using statement to work is to change it to ProjectName.WCFServiceUAT. Is there a way around this? – Cole9350 Aug 06 '15 at 16:16

1 Answers1

-3

See https://www.codeproject.com/Questions/233650/How-to-define-Global-veriable-in-Csharp-net

In project subdirectory App_Code, I created a file Globals.cs like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GlobalVariables
{

    /// <summary>
    /// Summary description for Globals
    /// See https://www.codeproject.com/Questions/233650/How-to-define-Global-veriable-in-Csharp-net
    /// </summary>
    public static class Globals
    {
        //  Note that now the obsolete code now only leaves one warning
        //  for the block excluded code:
        //      Unreachable code detected
        //  instead of a warning line for each instance of obsolete code.
        //  The "Unreachable code detected" can be disabled and enabled with 
        //  #pragma warning disable 162 
        //  #pragma warning enable 162 
        public const bool UAT = true;

        static Globals()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}

In the project .ascx.cs files, you don't need to have "using".

In the place where you want this to make a difference, you do this:

if (GlobalVariables.Globals.UAT)    //  See App_Code\Globals.cs
{
    //  Do the UAT stuff
}
else
{
    //  Do the other stuff
}

In the code that is NOT used, you get one warning: warning CS0162: Unreachable code Detected This message can be disabled:

#param warning disable 162
if (GlobalVariables.Globals.UAT)    //  See App_Code\Globals.cs
{
    //  Do the UAT stuff
}
else
{
    //  Do the other stuff
}
#param warning enable 162
andrewmcc
  • 85
  • 1
  • 1