我有以下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!";
}
}
}
答案 0 :(得分:5)
if-block的目的是什么?我可以看到,如果执行交换机下方的代码,StringNotFound
将始终为true
,因此if-block不是必需的,但它可能会很好地混淆代码分析。
答案 1 :(得分:4)
如果StringNotFound为false,则不返回任何内容。
答案 2 :(得分:3)
在方法的最后,如果StringNotFound
为false:
if(StringNotFound)
{
[...]
}
// missing return statement here
答案 3 :(得分:3)
也许您可以重构它,以便在代码的底部有一个SINGLE返回语句。所有决策代码都只是“设置”返回值。
然后在你的开关(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;
答案 4 :(得分:1)
为了防止这样的错误或谜题,最好不要同时执行这两个操作(下面),因为当你执行时不再有单一的逻辑流程:
retVal
变量
return
语句的多种用途。选择一个解决方案:
retVal
变量或答案 5 :(得分: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);
这更容易维护。
(正如其他人已经指出的那样,在方法的最后会有一个返回语句,并且布尔变量是多余的。)
答案 6 :(得分:0)
如果(StringNotFound)
如果这是假的,那么就没有其他法规可以捕获它。
使用
如果(StringNotFound)
{
//你的代码
}
否则
{
返回“未找到字符串!”;
}