【问题标题】:if and else if getting error with method return datatable c# [duplicate]if 和 else if 方法返回数据表 c# [重复]
【发布时间】:2018-09-24 03:36:59
【问题描述】:

当我使用 if 和 else if 语句编写返回数据表的方法时出现错误。什么是正确的格式。提前致谢 “并非所有代码路径都返回值”

    [HttpGet]
    public DataTable getcareerdata (int tclass, int efkey)
    {
        if (tclass ==7)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
            on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ", efkey));
        }
        else if (tclass == 8)
        {
            return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
            when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
            on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
            Order By  Scientific_Degree  , From_Value ",efkey));
        }
    }

【问题讨论】:

  • 在 tclass 不 = 7 或 8 的情况下,没有返回数据表。你如何处理这取决于你的需要。
  • 你需要一个包罗万象的else 吗?
  • 您可以在末尾添加return null 以避免编译错误。
  • 在类的顶部(在 if 之外)声明一个 DataTable,然后在 if 语句中填充该 Datatable,并在底部的 if 之外执行 RETURN。
  • 我认为您可以找到数千个此错误的重复项。下次发帖前先搜索一下吧

标签: c#


【解决方案1】:

无论如何你都需要返回一些东西,所以如果两个条件都不满足,则在 if 语句之外添加 return null。

[HttpGet]
public DataTable getcareerdata (int tclass, int efkey)
{
    if (tclass ==7)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
        on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ", efkey));
    }
    else if (tclass == 8)
    {
        return obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
        when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
        on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
        Order By  Scientific_Degree  , From_Value ",efkey));
    }
return null;
}

【讨论】:

    【解决方案2】:

    如果你的两个 if 条件都失败了,那么你的方法没有任何东西可以返回,而你的方法签名告诉编译器它将返回一个 DataTable 类型的对象,在上述情况下它将无法返回。

    所以,应该做的是使用DataTable 类型的局部变量并在条件块中相应地设置它的值,然后最终从方法返回变量。

    将您的方法重构为:

    DataTable table = null;
    if (tclass ==7)
    {
        table  = obj.GetData(string.Format(@"select Effect_Class_Key , From_Value , To_Value , Effect_Class_Value, Scientific_Degree, case When Scientific_Degree  is null then 'Not Related'
                when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else latin_desc  end as Spec from hr_effect_classes Left Outer Join general_cod 
                on hr_effect_classes.Scientific_Degree = general_cod .sub_cod and  main_cod = 17001  where type_class in (7) and status_class = 1  and effect_key = {0}
                Order By  Scientific_Degree  , From_Value ", efkey));
    }
    else if (tclass == 8)
    {
        table  = obj.GetData(string.Format(@"select Effect_Class_Key ,  From_Value , To_Value , Effect_Class_Value, Scientific_Degree case When Scientific_Degree is null then 'Not Related'
                when Scientific_Degree  = 0 then 'Not Related' when Scientific_Degree  = -1 then 'Not Related' else L_Desc end as Spec from hr_effect_classes Left Outer Join HR_Specialty   
                on hr_effect_classes.Scientific_Degree = HR_Specialty.Spec_Key where type_class in (8) and status_class = 1 and effect_key = {0}
                Order By  Scientific_Degree  , From_Value ",efkey));
    }
    
     return table;
    

    现在我们返回 DataTable 类型的引用,尽管如果两个条件都失败,它将为 null,但它会使编译器认为您的代码有效,而不会出现任何构建时错误。

    【讨论】:

      猜你喜欢
      • 2013-09-09
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-08
      • 2022-07-12
      • 2018-07-10
      • 2018-03-14
      相关资源
      最近更新 更多