解决此返回路径问题 [英] solve this return path problem

查看:65
本文介绍了解决此返回路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友
下面的代码给出了错误,因为所有代码都必须具有返回路径.我可以在哪里放置returnpath?

public List<string> bc11()
        {
            try
            {
                Dob.open();
                SqlCommand cmd6 = new SqlCommand("sp_bc1", Dob.con);
                cmd6.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader dr = cmd6.ExecuteReader();
                List<string> bi = new List<string>();
                
                
                    while (dr.Read())
                    {
                      
                        bi.Add(dr["Schoolname"].ToString());
                        return bi;
                    }
               
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                if (Dob.con != null)
                {
                    Dob.close();
                }
            }

解决方案

需要在while循环后添加返回值:

  while (dr.Read())
  {
      bi.Add(dr [" ].ToString());
  }
     返回 bi; 



现在,bi将在while循环的第一个循环之后返回.不要以为这就是您想要的.


while循环之后,您需要一个return语句.请考虑一下,如果出于某些原因dr.Read()false开头,则循环内的代码将永远不会被命中,并且该函数将在不返回任何内容的情况下结束,这仅在void函数中有效.

但是,为什么即使在循环中也是如此?它将在第一次迭代后离开该函数,因此if而不是while将具有相同的结果.例如,如果在dr上没有记录,则您的return bi将不会执行,因此该函数将没有返回值.这就是消息告诉您的内容.

我更喜欢每个函数一个返回语句.因此,在函数的开头声明一个类型为List< string>的新变量.然后在您的while语句中将其设置为bi.最后,然后返回新变量.这样,无论函数返回什么值.


Hello friends
the below code give an error as all code must have return path.where can i put returnpath?

public List<string> bc11()
        {
            try
            {
                Dob.open();
                SqlCommand cmd6 = new SqlCommand("sp_bc1", Dob.con);
                cmd6.CommandType = CommandType.StoredProcedure;
                
                SqlDataReader dr = cmd6.ExecuteReader();
                List<string> bi = new List<string>();
                
                
                    while (dr.Read())
                    {
                      
                        bi.Add(dr["Schoolname"].ToString());
                        return bi;
                    }
               
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                if (Dob.con != null)
                {
                    Dob.close();
                }
            }

解决方案

Need to add the return after the while loop:

while (dr.Read())
  {
      bi.Add(dr["Schoolname"].ToString());
  }
     return bi;



The way you have it now, the bi will be returned after the first loop of the while loop. Don''t think that is what you want.


You need a return statement after the while loop. Think about it, if for some reason dr.Read() starts as false, the code inside the loop is never hit, and the function ends without anything being returned, which is not valid except in a void function.

But why is that even in a loop at all? It leaves the function after the first iteration, so if instead of while would have the same result.


Not all of the paths the code could take return a value. For example, if there are no records on dr your return bi will not execute and so the function will not have a return value. That is what the message is telling you.

I prefer a single return statement per function. So, at the beginning of your function declare a new variable of type List<string> and then set it to bi in your while statement. Then return the new variable after your finally. That way, no matter what the function returns a value.


这篇关于解决此返回路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆