C# if和else if使用返回数据表c的方法获取错误#
当我使用if和elseif语句编写返回数据表的方法时,我遇到了一个错误。正确的格式是什么。提前谢谢 “并非所有代码路径都返回值”C# if和else if使用返回数据表c的方法获取错误#,c#,C#,当我使用if和elseif语句编写返回数据表的方法时,我遇到了一个错误。正确的格式是什么。提前谢谢 “并非所有代码路径都返回值” 如果您的两个if条件都失败,那么您的方法没有任何可返回的内容,而您的方法签名告诉编译器它将返回类型为DataTable的对象,在上述情况下,它将无法返回该对象 所以,应该做的是使用一个类型为DataTable的局部变量,在条件块中相应地设置它的值,然后从方法返回变量 重构您的方法,使其类似于: DataTable table = null; if (tclass ==
如果您的两个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,但它将使编译器认为有效的代码没有任何构建时错误。无论发生什么情况,您都需要始终返回某些内容,因此如果两个条件都不满足,请在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;
}
在tclass不=7或8的情况下,没有返回数据表。如何处理取决于您需要什么。是否需要一个catch all
else
?您可以在末尾添加一个return null
,以避免编译错误。在类的顶部声明一个数据表(在if之外),然后在if语句中填充该数据表,并在底部的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));
}
return null;
}