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

7 に答える 7

5

if-blockの目的は何ですか?私が見る限り、スイッチの下のコードが実行されるStringNotFound場合true、if-blockは必要ありませんが、コード分析を非常に混乱させる可能性があります。

于 2012-11-28T13:54:49.353 に答える
4

StringNotFound が false の場合、何も返されません。

于 2012-11-28T13:48:05.763 に答える
3

たぶん、コードの下部にSINGLE returnステートメントがあるように、リファクタリングすることができます。そして、すべての意思決定コードは、単に戻り値を「設定」します。

次に、switch(Name)ブロックで、返したいものの値を設定します。次に、(大量の戻り値を取得するのではなく)スイッチから抜け出します。IMO、これでコードがすっきりします。また、メンテナンスもしやすくなると思います。

すなわち。

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;
于 2012-11-30T10:57:42.263 に答える
3

メソッドの最後で、ifStringNotFoundが false の場合:

if(StringNotFound)
{
   [...]
}

// missing return statement here
于 2012-11-28T13:48:13.910 に答える
1

このような間違いやパズルを防ぐには、次の場合に単一の論理フローがなくなるため、この両方(以下)を実行しない方がよいでしょう。

  • retValと一緒に変数を維持する
  • returnステートメントの複数の使用。

1つの解決策を選択してください:

  • retVal変数のみを返す、または
  • 関数の下部にデフォルトを返します。
于 2012-11-28T13:50:17.043 に答える
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);

これは保守が容易です。

(他の人がすでに指摘しているように、メソッドの最後に return-statement がなく、ブール変数は不要です。)

于 2012-11-28T14:08:37.390 に答える
0

if(StringNotFound)

これが偽の場合、それを捕らえるための他のステートメントはありません。

使用する

if(StringNotFound)
{
//コード
}
Else
{
return "String not found!";
}

于 2012-11-28T13:53:26.077 に答える