C# 并非所有代码路径都返回值
我有一个很大的问题。我的CreateArmy方法不断返回错误:并非所有代码路径都返回值。我肯定它们都返回一个值:C# 并非所有代码路径都返回值,c#,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. S
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);
}
}
您的方法应该返回一个字符串
public string CreateArmy(string inputFile)
然而,现在在您的方法中有一个这样做的return语句
如果不需要return语句,则使方法无效,即不返回:
public void CreateArmy(string inputFile)
否则,请确保从所有可能的代码路径返回正确的数据。不要将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);
}
}
您需要一个return语句来在Try块中以及Catch块中返回字符串,以防抛出异常
如果您不想从方法调用返回任何内容,请将方法的返回类型设置为void。您已声明该方法将返回字符串,但它不会。您没有一个返回语句。显示messagebox与返回字符串不同。代码中没有return语句…因此它不会返回任何内容。臭名昭著的返回会使您处于空白中…或…永远不会返回的空白。。。