Asp.net &引用;并非所有代码路径都返回一个值";

Asp.net &引用;并非所有代码路径都返回一个值";,asp.net,xml,c#-4.0,Asp.net,Xml,C# 4.0,我的问题在于此代码的CreateXML()部分。被回退的错误是 .GenerateXML.CreateXml()的电子归档:并非所有代码路径都返回值 public ElectronicRecordAppellateCase CreateXml() { ElectronicRecordAppellateCase output=new ElectronicRecordAppellateCase(); if(someVariableAlreadyDefined>otherVariabl

我的问题在于此代码的CreateXML()部分。被回退的错误是

.GenerateXML.CreateXml()的电子归档:并非所有代码路径都返回值

public ElectronicRecordAppellateCase CreateXml()
{
  ElectronicRecordAppellateCase  output=new ElectronicRecordAppellateCase();
  if(someVariableAlreadyDefined>otherVariable)
  {
    return output;
  } 
  return null;   //you can return the object here as needed
}
我尝试过不同的方法,但结果相同


专业人士有什么线索吗?

如果您指定了输出类型,那么您的方法必须在代码的每个路径后面提供一个值。当您看到此错误时,意味着方法中的一个或多个方案不返回指定类型的值,而是导致方法终止

这是此类有问题方法的一个示例:

namespace Electronic_Filing_of_Appeals
{
public class GenerateXML
{
  public ElectronicRecordAppellateCase CreateXml()
  {
这可以这样解决,例如:

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

查看模式?

如果指定输出类型,则方法必须在代码的每个路径后面提供一个值。当您看到此错误时,意味着方法中的一个或多个方案不返回指定类型的值,而是导致方法终止

这是此类有问题方法的一个示例:

namespace Electronic_Filing_of_Appeals
{
public class GenerateXML
{
  public ElectronicRecordAppellateCase CreateXml()
  {
这可以这样解决,例如:

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

看到模式了吗?

您的方法被支持返回
ElectronicRecordAccelerCase
类的实例。我猜您是在方法中的某个If条件下返回结果,或者类似这样

public ElectronicRecordAppellateCase CreateXml()
{
    if (something)
    {
       return new ElectronicRecordAppellateCase();
    }
    else return null; // "else" isn't really needed here
}
解决方案:确保从方法返回有效的返回值

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!!!!

}

我们支持您的方法返回
ElectronicRecordAppealtecase
类的实例。我猜您是在方法中的某个If条件下返回结果,或者类似这样

public ElectronicRecordAppellateCase CreateXml()
{
    if (something)
    {
       return new ElectronicRecordAppellateCase();
    }
    else return null; // "else" isn't really needed here
}
解决方案:确保从方法返回有效的返回值

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!!!!

}

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

你没有显示你的代码,所以我举了个例子

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

public ElectronicRecordAppellateCase CreateXml()
{
  ElectronicRecordAppellateCase  output=new ElectronicRecordAppellateCase();
  if(someVariableAlreadyDefined>otherVariable)
  {
    return output;
  } 
  return null;   //you can return the object here as needed
}

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

你没有显示你的代码,所以我举了个例子

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

public ElectronicRecordAppellateCase CreateXml()
{
  ElectronicRecordAppellateCase  output=new ElectronicRecordAppellateCase();
  if(someVariableAlreadyDefined>otherVariable)
  {
    return output;
  } 
  return null;   //you can return the object here as needed
}

显示方法本身,而不仅仅是定义…无法从您发布的内容中分辨出来。该错误意味着存在一个不返回任何内容的代码路径(缺少
else
case
等)。我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。展示方法本身,而不仅仅是定义……从你发布的内容无法判断。该错误意味着存在一个不返回任何内容的代码路径(缺少
else
case
等)。我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。感谢大家的快速反应。我意识到我忘记了代码末尾的“return”。你们都很摇滚!谢谢大家的快速回复。我意识到我忘记了代码末尾的“return”。你们都很摇滚!