2013-12-10 56 views
0

我遇到了一個很大的問題。我的CreateArmy方法不斷返回錯誤:並非所有的代碼路徑都返回一個值。我敢肯定他們都返回一個值:C#不是所有的代碼路徑都返回一個值CreateArmy

public string CreateArmy(string inputFile) 
{ 
    string grabFile = inputFile + ".txt"; 
    int counter = 0; 
    string line; 
    try 
    { 
     // Read the file and display it line by line. 
     StreamReader file = new StreamReader(grabFile); 

     while ((line = file.ReadLine()) != null) 
     { 
     char[] fixedCommands = line.Remove(0, 3).ToCharArray(); 
     commands[0] = fixedCommands[0]; 
     commands[1] = fixedCommands[1]; 
     commands[2] = fixedCommands[2]; 
     byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine); 
     commands[].Write(newline, 0, newline.Length); 
     counter++; 
     } 
     char[] newcommands = new string(commands).Remove(0, 3).ToCharArray(); 

     file.Close(); 
     MessageBox.Show("There are " + counter + " robots!"); 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show("Please tell me the .txt file type. Do not include the .txt extension."); 
     MessageBox.Show(e.Message); 
    } 
} 
+2

你已經聲明該方法將返回一個字符串,但它沒有。你沒有一個'return'語句。顯示一個消息框與返回一個字符串不一樣。 –

+0

代碼中沒有return語句,所以它不會返回任何內容。 – FloChanz

+0

臭名昭着的回報會讓你陷入「空白」......或者......永遠不會返回的「空白」...... – terrybozzio

回答

0

你的方法應該返回一個字符串

public string CreateArmy(string inputFile) 

然而,nowehre在你的方法是有一個return聲明這樣做。

如果不需要return語句,則使該方法孔隙率(即不歸):

public void CreateArmy(string inputFile) 

否則,確保正確的數據從返回的所有可能的代碼路徑。

0

要麼不設置CreateArmy()函數來返回一個字符串,要麼給你的代碼添加一個return語句。

解決方案#1:

public void CreateArmy(string inputFile) 
     { 
     string grabFile = inputFile + ".txt"; 
     int counter = 0; 
     string line; 
     try 
     { 
      // Read the file and display it line by line. 
      StreamReader file = new StreamReader(grabFile); 

     while ((line = file.ReadLine()) != null) 
     { 
      char[] fixedCommands = line.Remove(0, 3).ToCharArray(); 
      commands[0] = fixedCommands[0]; 
      commands[1] = fixedCommands[1]; 
      commands[2] = fixedCommands[2]; 
      byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine); 
      commands[].Write(newline, 0, newline.Length); 
      counter++; 
     } 

     char[] newcommands = new string(commands).Remove(0, 3).ToCharArray(); 

     file.Close(); 

     MessageBox.Show("There are " + counter + " robots!"); 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show("Please tell me the .txt file type. Do not include the .txt extension."); 
     MessageBox.Show(e.Message); 
    } 
} 

解決方案2:

public string CreateArmy(string inputFile) 
     { 
     string grabFile = inputFile + ".txt"; 
     int counter = 0; 
     string line; 
     try 
     { 
      // Read the file and display it line by line. 
      StreamReader file = new StreamReader(grabFile); 

     while ((line = file.ReadLine()) != null) 
     { 
      char[] fixedCommands = line.Remove(0, 3).ToCharArray(); 
      commands[0] = fixedCommands[0]; 
      commands[1] = fixedCommands[1]; 
      commands[2] = fixedCommands[2]; 
      byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine); 
      commands[].Write(newline, 0, newline.Length); 
      counter++; 
     } 

     char[] newcommands = new string(commands).Remove(0, 3).ToCharArray(); 

     file.Close(); 

     MessageBox.Show("There are " + counter + " robots!"); 

     return null; // OR return some other string value! 
    } 
    catch (Exception e) 
    { 
     MessageBox.Show("Please tell me the .txt file type. Do not include the .txt extension."); 
     MessageBox.Show(e.Message); 
    } 
} 
0

您將需要一個return語句在 「嘗試」 塊返回一個字符串,以及在您的「Catch」塊以防引發異常。

如果您不是要從方法調用返回任何內容,請將方法的返回類型設置爲「void」。

相關問題