0
namespace Electronic_Filing_of_Appeals
{
public class GenerateXML
{
  public ElectronicRecordAppellateCase CreateXml()
  {

我在这段代码的 CreateXML() 部分撒谎。被踢回来的错误是

Electronic_Filing_of_Appeals.GenerateXML.CreateXml():并非所有代码路径都返回值

我尝试了不同的方法,但结果相同。

专业人士有什么线索吗?

4

3 回答 3

2

If you specify output type, your method HAS to provide a value following every path of the code. When you see this error, it means one or more scenarios in your method don't return a value of a specified type, but result in a termination of the method instead.

This is an example of such problematic method:

public ElectronicRecordAppellateCase CreateXml()
{
    if (something)
    {
       return new ElectronicRecordAppellateCase();
    }
    // if the something is false, the method doesn't provide any output value!!!
}

This could be solved like this for instance:

public ElectronicRecordAppellateCase CreateXml()
{
    if (something)
    {
       return new ElectronicRecordAppellateCase();
    }
    else return null; // "else" isn't really needed here
}

See the pattern?

于 2013-06-05T20:50:46.217 回答
2

Your method is suppoed to return an instance of ElectronicRecordAppellateCase class. I guess you are returning the result in some If condition in your method or so like this.

public ElectronicRecordAppellateCase CreateXml()
{
  ElectronicRecordAppellateCase  output=new ElectronicRecordAppellateCase();
  if(someVariableAlreadyDefined>otherVariable)
  {
    //do something useful
    return output;
  }

 // Not returning anything if the if condition is not true!!!!

}

Solution : Make sure you are returning a valid return value from the method.

public ElectronicRecordAppellateCase CreateXml()
{
  ElectronicRecordAppellateCase  output=new ElectronicRecordAppellateCase();
  if(someVariableAlreadyDefined>otherVariable)
  {
    return output;
  } 
  return null;   //you can return the object here as needed
}
于 2013-06-05T20:51:30.453 回答
1

并非所有代码路径都返回值意味着,您的函数可能不会返回预期值

你没有显示你的代码所以我做了一个例子

例如,follow 函数有 3 条路径,如果 parm 等于 1,如果 parm 等于 2 但如果 parm 不等于 1 或 2,则不返回值

function SomeObject foo(integer parm){

    if (parm == 1) {
        return new SomeObject();
    }
    if (parm == 2) {
        return new SomeObject();
    }
    //What if parm equal something else???
}
于 2013-06-05T20:52:34.293 回答