2014-10-06 56 views
0

我已经写了一个函数来通过搜索文本来找到该行,并且在找到特定的行后,我想读取下一行并返回该文本。功能如下:不是所有的代码路径都返回一个值

public static string NextString(string textfind) 
{ 
    List<string> found = new List<string>(); 
    string linejd; 

    using (StreamReader efile = new StreamReader(FILENAME)) 
    { 
     int counter = 0; 
     while ((linejd = efile.ReadLine()) != null) 
     { 
      counter++; 
      if (linejd.Contains(textfind)) 
      { 
       found.Add(linejd); 
       string nextstring = efile.ReadLine(); 
       return nextstring; 
      } 
     } 
    } 
} 

文件名已被定义为:

const string FILENAME = @"E:\model\Yen and Lee\AQRun01\eratc.inp"; 

不过,我不断收到错误:

AQ.Program.NextString(string)': not all code paths return a value

+0

放'返回nextstring;''外使用(声明){}'。 – 2014-10-06 23:12:13

+1

该方法在不进入while循环时应该返回什么? – abatishchev 2014-10-06 23:20:43

回答

2

如果在上述函数中,您的代码不会进入循环或if (linejd.Contains(textfind))块?该函数没有返回值!

我倾向于建议声明一个函数的结果变量和函数内设置的值,然后在最后返回它:

static public string nextstring(string textfind) 
{ 
    string result = string.Empty; 

    List<string> found = new List<string>(); 
    string linejd; 

    /* ******************************************************** 
    * Find the line with certain string */ 

    using (StreamReader efile = new StreamReader(FILENAME)) 
    // using (efile) 
    { 
     int counter = 0; 
     while ((linejd = efile.ReadLine()) != null 
       && string.IsNullOrWhiteSpace(result)) // Quit the loop once we have a result! 
     { 
      counter++; 
      if (linejd.Contains(textfind)) 
      { 
       found.Add(linejd); 
       string nextstring = efile.ReadLine(); 
       result = nextstring;    } 
     } 
    } 
    return result; 
} 
+0

虽然此方法可行(并且满足尽可能少返回语句的'最佳实践'),但如果循环在已找到答案后执行大量迭代,则效率会非常低下。 – 2014-10-06 23:17:20

+1

已经添加了退出检查。使用方法 – 2014-10-06 23:17:54

+0

了两个错误:'“错误‘串’不包含定义“IsNullOrWhitespace'' \t和'错误非可调用成员‘的String.Empty’不能等的方法使用。 \t' – 2014-10-06 23:21:18

1

如果条件linejd.Contains(textfind)是不正确的,然后函数永远不会返回任何东西,但函数声明指出它将返回一个字符串。你可以通过在using块后面返回一个默认值(比如一个空字符串)来解决这个问题。

1

有两个原因,为什么功能可以在不退出并返回值:

  1. 该文件为空,因此不会输入while循环。
  2. 条件linejd.Contains(textfind)从不是true

即使您知道该文件永远不会为空,并且该文件中始终可以找到该字符串,编译器也不会知道这一点。 (虽然while循环没有意义,如果你知道该字符串总是可以找到,因为这意味着你永远不会到达文件的末尾)。

你需要告诉编译器该做什么对于这两种情况,例如在函数结尾添加return null;

或者重写代码,以便它实际上依赖于始终包含某些内容的文件,并且始终找到该字符串。这样就没有松散的结局要照顾。这当然意味着如果文件实际上是空的或者没有找到字符串,代码会崩溃或挂起。

+0

感谢你的建议,它真的有帮助 – 2014-10-06 23:36:37

1

如何使用Linq?

public static string NextString(string textfind) 
{ 
    return File.ReadLines(FILENAME) 
      .SkipWhile(line => !line.Contains(textfind)) 
      .Skip(1) 
      .First(); 
} 
+0

完美的作品,谢谢你的选择。 – 2014-10-06 23:30:42

0

当if case总是出错的话,那么你的方法将不会返回任何东西..这就是为什么你会得到一个错误。 一下你的方法像在年底前写返回值:

public static string NextString(string textfind) 
{ 
List<string> found = new List<string>(); 
string linejd; 
string new_string = string.Empty; 

using (StreamReader efile = new StreamReader(FILENAME)) 
{ 
    int counter = 0; 
    while ((linejd = efile.ReadLine()) != null) 
    { 
     counter++; 
     if (linejd.Contains(textfind)) 
     { 
      found.Add(linejd); 
      string nextstring = efile.ReadLine(); 
      return nextstring; 
     } 
    } 
} 


return (new_string); 

}

相关问题