2015-08-27 35 views
1

我想在VS2010,4.0框架中調試現有的應用程序。我得到這個編譯時錯誤:C#預處理器指令(#if和#endif)不起作用。編譯錯誤

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

我看不出什麼錯在下面的代碼:

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; } } 
    } 
} 

TResourcesEnResources是資源文件(的.resx)

我錯過了一些有關.Net的參考?

+6

大概估計既沒有'TR'也沒有'EN'被定義...這並不是你通常做全球化的方式。你不建立單獨的版本 - 你建立*一個*版本,在執行時加載適當的資源。 –

回答

0

爲了編譯代碼,您需要在Project屬性的Build設置中定義TR或EN。目前沒有定義,所以b_resources永遠不會被定義。

2

最明顯的問題是:您是否在當前版本中定義了編譯符號TREN?如果不是,則#IF語句將爲false,並且using語句將不包含在內。換句話說,如果你不定義這些符號,下面的代碼會被編譯:

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; } } 
    } 
} 

要檢查或設置編譯符號,去你的項目的屬性。然後在Build選項卡上,您將看到名爲Conditional編譯符號的文本框。檢查符號是否在那裏定義。如果沒有,請在其中添加一個。

如果您使用的是Visual Studio,則實際上可以立即查看是否屬於這種情況。如果未爲當前版本定義符號,則#IF塊中的代碼將變灰。如果符號被定義,它將像普通代碼一樣顯示jsut。

1

您沒有默認情況,所以如果既沒有定義TR或EN,也沒有定義b_resources。你需要有一些其他的情況,所以它可以隨時編譯。例如,如果你想你的EN資源爲默認:如果沒有TR或EN被定義最終都不會被列入

#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