locked
Conditional Compilation RRS feed

  • Question

  • Hi, I have a number of projects in my solution and I use Conditional Compilation in a number of places, I would like to share Constants between projects, so I don’t have to define and undefined the same Constant in a number of projects. Can't seem to find a way round this.

    Any comments appreciated

    Thanks
    Thursday, April 27, 2006 10:47 AM

Answers

  • You can define constants so that are accessible by the entire project, by doing the following in Visual Basic 2005:

    1. In Solution Explorer, right-click your project and choose Properties
    2. In the Properties window, choose the Compile tab and click Advanced Compile Options
    3. In the Custom constants text box, enter your constant.

    Regards

    David

    Thursday, April 27, 2006 2:17 PM
  • There is no way via Visual Studio to do this, however, you can achieve this by having each individual project import a common MSBuild targets file that defines the constant.

    1. Create a new text file called Common.targets along side your solution (*.sln) and paste the following:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       
    <PropertyGroup
    >
           
    <DefineConstants>MyConstant="Value"</DefineConstants
    >
        </
    PropertyGroup
    >
    </Project>

    2. Open each project file (*.vbproj) with a text editor and paste the following bolded line where indicated:


    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       
    <Import Project="..\Common.targets" />
        <PropertyGroup>

        [...]

    This will cause each project to pick up the constant.

    Sunday, June 4, 2006 4:20 PM

All replies

  • You can define constants so that are accessible by the entire project, by doing the following in Visual Basic 2005:

    1. In Solution Explorer, right-click your project and choose Properties
    2. In the Properties window, choose the Compile tab and click Advanced Compile Options
    3. In the Custom constants text box, enter your constant.

    Regards

    David

    Thursday, April 27, 2006 2:17 PM
  • Thanks for the reply David.

     

    The problem is that Custom Constants are private to the project, what I need is Solution wide constants. I need access to the same constant, in a number of projects. Hope this makes sense

     

    Julian

    Saturday, April 29, 2006 11:44 AM
  • There is no way via Visual Studio to do this, however, you can achieve this by having each individual project import a common MSBuild targets file that defines the constant.

    1. Create a new text file called Common.targets along side your solution (*.sln) and paste the following:

    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       
    <PropertyGroup
    >
           
    <DefineConstants>MyConstant="Value"</DefineConstants
    >
        </
    PropertyGroup
    >
    </Project>

    2. Open each project file (*.vbproj) with a text editor and paste the following bolded line where indicated:


    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
       
    <Import Project="..\Common.targets" />
        <PropertyGroup>

        [...]

    This will cause each project to pick up the constant.

    Sunday, June 4, 2006 4:20 PM