RNat
RNat

Reputation: 35

C# Preprocessor Directives (#if and #endif) not working. Compilation error

I am trying to debug an existing application in VS2010, 4.0 framework. I get this compile-time error:

"The name 'b_resources' does not exist in the current context" .

I cannot see anything wrong in the code below:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;

#if TR
    using b_resources = Common.Resources.TResources;
#endif

#if EN
    using b_resources = Common.Resources.EnResources;
#endif

namespace Common
{
    public static class BResources
    {

        public static string caption { get { return b_resources.ProductName; } }
        public static string company_name { get { return b_resources.CompanyName; } }
    }
}

TResources and EnResources are resource files (.resx)

Am I missing some references related to .Net ?

Upvotes: 2

Views: 1696

Answers (3)

user957902
user957902

Reputation: 3060

You have no default case so if neither TR or EN is defined you get no definition of b_resources. You need to have some else cases in there so it can always compile. For example, if you wanted your EN resource as default:

#if TR
    using b_resources = Common.Resources.TResources;
#elif EN
    using b_resources = Common.Resources.EnResources;
#else
    using b_resources = Common.Resources.EnResources; // or whatever you want as default
#endif

If neither TR or EN are defined the final else will be included.

Upvotes: 1

Erik Schierboom
Erik Schierboom

Reputation: 16646

The most obvious question is: do you define the TR or EN compilation symbol in your current build? If not, both #IF statements will be false and the using statement won't be included. In other words, if you do not define those symbols, the following code will be compiled:

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;

// Note: missing using statements

namespace Common
{
    public static class BResources
    {

        public static string caption { get { return b_resources.ProductName; } }
        public static string company_name{ get { return b_resources.CompanyName; } }
    }
}

To check or set compilation symbols, go to your project's properties. Then on the Build tab you see as textbox named Conditional compilation symbols. Check if the symbols are defined there. If not, add one of them there.

If you are using Visual Studio, you can in fact immediately see if this is the case. If a symbol is not defined for the current build, the code within the #IF block will be greyed out. If the symbol is defined, it will display jsut like normal code.

Upvotes: 3

Russ
Russ

Reputation: 4173

For your code to compile, you need to define either TR or EN in the Build settings of your Project properties. Currently neither are defined, so b_resources never gets defined.

Upvotes: 0

Related Questions