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 块的目的是什么?据我所知,如果执行开关下面的代码,StringNotFound将始终true如此,因此不需要 if 块,但它很可能会混淆代码分析。

于 2012-11-28T13:54:49.353 回答
4

如果 StringNotFound 为 false,则不返回任何内容。

于 2012-11-28T13:48:05.763 回答
3

在你的方法的最后,如果StringNotFound是假的:

if(StringNotFound)
{
   [...]
}

// missing return statement here
于 2012-11-28T13:48:13.910 回答
3

也许您可以重构它,以便在代码底部有一个 SINGLE return 语句。你所有的决策代码只是简单地“设置”返回值。

然后在你的 switch(Name) 块中设置你想要返回的值 - 然后打破开关(而不是有一大堆返回)。IMO,这将使代码更整洁。另外,我认为它会更容易维护。

IE。

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 回答
1

为了防止这样的错误或谜题,你最好不要同时做这两个(如下),因为当你:

  • 维护一个retVal变量,连同
  • return该语句的多种用途。

选择一种解决方案:

  • 只返回一个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);

这更容易维护。

(正如其他人已经指出的那样,在您的方法的最后缺少一个返回语句,并且布尔变量是多余的。)

于 2012-11-28T14:08:37.390 回答
0

如果(字符串未找到)

如果这是错误的,则没有其他声明可以抓住它。

采用

if(StringNotFound)
{
//你的代码
}
Else
{
return "String not found!";
}

于 2012-11-28T13:53:26.077 回答