2012-11-28 48 views
0

我有以下的C#代碼。由於我不會介入的原因,這是本地化的必要方式。並非所有的代碼路徑都返回一個值 - 在哪裏?

我的問題是,我不能爲我的生活找出什麼路徑沒有返回一個值。在下面的代碼中沒有其他錯誤:

ResourceManager ResMain = new ResourceManager("TorrentSched.Main", typeof(Main).Assembly); 
/// <summary>Returns a localised string based on the Current UI Culture</summary> 
public string Str(string Name) 
{ 
    Boolean StringNotFound = false; 
    switch(Thread.CurrentThread.CurrentUICulture.ToString()) 
    { 
     case "en-GB": 
      switch(Name) 
      { 
       case "MinimizeToTray": return "Closing the program minimises it to the tray. To fully quit the program, right-click the icon in your tray and choose Exit."; 
       default: StringNotFound = true; break; 
      } 
     break; 
     default: 
      StringNotFound = true; 
     break; 
    } 

    if(StringNotFound) 
    { 
     StringNotFound = false; 
     switch(Name) 
     { 
      case "AppName":   return ResMain.GetString("$this.Text"); 
      case "MinimizeToTray": return "Closing the program minimizes it to the tray. To fully quit the program, right-click the icon in your tray and choose Exit."; 
      case "ReallyExit1":  return "Do you really want to exit?"; 
      case "ReallyExit2":  return "Torrents will not be checked and downloaded until you launch the program again!"; 
      default:    return "String not found!"; 
     } 
    } 
} 
+4

呈現的功能**不完整**。我猜,代碼的底部存在問題。 – Tigran

+0

@Tigran我錯過了一個'}' - 編輯。 –

+2

if-block的用途是什麼?就我所知,如果執行下面的代碼,'StringNotFound'將始終爲真,因此if-block不是必需的,但它可能會混淆代碼分析。 –

回答

5

if-block的用途是什麼?據我所見,如果執行下面的代碼,StringNotFound將始終爲true,因此if-block不是必需的,但它可能會混淆代碼分析。

+0

完美的答案。奇怪的是,Visual Studio沒有意識到'StringNotFound'永遠不會是錯誤的,因此所有的代碼路徑都會返回一個值。 –

+0

@DannyBeckett:做這樣的分析很難做,並且會減慢編譯時間(請參閱「停止問題」)。所以這是精確度和時間的妥協。在這裏,編譯器在真塊中看到返回,並且在沒有最終返回的(missing)else塊中沒有等價物。所以它假定有一條可能沒有返回值的路徑。它不夠聰明(通過設計)來靜態分析代碼。 – Skizz

4

如果StringNotFound爲false,則不返回任何內容。

3

在你的方法的盡頭,如果StringNotFound是假的:

if(StringNotFound) 
{ 
    [...] 
} 

// missing return statement here 
1

爲了防止錯誤或拼圖這樣,你最好不要兩者都做這個(下圖),因爲沒有一個統一的邏輯流程再當你:

  • 保持一個retVal變量,加上
  • 多的return聲明的用途。

選擇一個解決辦法:

  • 僅返回retVal變量,或
  • 返回在函數的底部的默認值。
0

如果(StringNotFound)

如果這是假的,沒有別的statment抓住它。

使用

如果(StringNotFound)
{
//你的代碼
}
否則
{
回報 「字符串沒有找到!」;
}

+0

此代碼有錯誤。它的單個字符,所以我不能提出編輯。 –

+0

你應該現在做更多的senes :) – EllisChadwick

1

考慮使用字典

private static Dictionary<string,string> stringDict = new Dictionary<string,string>(); 

添加字符串

// Add default strings 
stringDict.Add("AppName", ResMain.GetString("$this.Text")); 
stringDict.Add("MinimizeToTray", "Closing the program ..."); 
stringDict.Add("ReallyExit1", "Do you really ..."); 

// Add culture specific strings 
stringDict.Add("en-GB;AppName", ResMain.GetString("$this.Text")); 
stringDict.Add("en-GB;MinimizeToTray", "Closing the program ..."); 
stringDict.Add("en-GB;ReallyExit1", "Do you really ..."); 

的你可以用

// Get culture specific string 
string culture = Thread.CurrentThread.CurrentUICulture.ToString(); 
string s; 
If (stringDict.TryGetValue(culture + ";" + Name, out s)) { 
    return s; 
}       

// Get default 
If (stringDict.TryGetValue(Name, out s)) { 
    return s; 
} 

return String.Format("String '{0}' not found!", Name); 

這是更易於維護得到琴絃非常快。 (正如其他人已經指出的那樣,在你的方法的最後有一個返回語句丟失,並且布爾變量是多餘的。)

3

也許你可以重構它,以便在代碼的底部有一個單一的return語句。而所有的決策代碼只是「設定」了返回值。

然後在您的開關(名稱)塊中設置您想返回的值 - 然後跳出開關(而不是一大堆回報)。國際海事組織,這將使代碼整潔。另外,我認爲這樣會更容易維護。

即。

switch(Name) 
     { 
      case "AppName":   retString = ResMain.GetString("$this.Text"); 
      case "MinimizeToTray": retString = "Closing the program minimizes it to the tray. To fully quit the program, right-click the icon in your tray and choose Exit."; 
      case "ReallyExit1":  retString = "Do you really want to exit?"; 
      case "ReallyExit2":  retString = "Torrents will not be checked and downloaded until you launch the program again!"; 
      default:    retString = "String not found!"; 
     } 

... 

return retString; 
相關問題