Not all code paths return a value - where?

前端 未结 7 1193
我在风中等你
我在风中等你 2021-01-26 06:03

I have the following C# code. For reasons I won\'t go into, this is the required way of localising.

My problem is, I cannot for the life of me figure out what path is no

7条回答
  •  -上瘾入骨i
    2021-01-26 06:43

    Consider using a dictionary

    private static Dictionary stringDict = new Dictionary();
    

    Add the strings

    // 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 ...");
    

    The you can get the strings very quickly with

    // 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);
    

    This is easier to maintain.

    (As other have already pointed out, there is a return-statement missing at the very end of your method and the boolean variable is superfluous.)

提交回复
热议问题