并非所有代码路径都返回值错误

时间:2016-02-10 16:52:38

标签: c# methods visual-studio-2015

我的方法有问题。环境提示我"并非所有代码路径都返回值"。我不明白为什么。我没有看到这条没有返回价值的道路。有人能解释一下为什么吗?谢谢和问候。

我正在使用Visual Studio 2015社区

private CognexDataMan.EnumCodeKind CheckCode(String strCodeToCheck)
{
    String[] splitCode = strCodeToCheck.Split(new char[] { ',' });
    String strCode = splitCode[1];

    if (strCode == String.Empty)
    {
        // code incorrect
        return CognexDataMan.EnumCodeKind.CODE_INCORRECT;
    }
    else
    {
        CognexDataMan.EnumCodeKind resultToReturn;

        SqlConnection sqlCnn = new SqlConnection(GlobalData.CnnString);
        SqlCommand sqlCmd = new SqlCommand("sp_CheckCode", sqlCnn);
        sqlCmd.Parameters.AddWithValue("@Code", strCode); 
        sqlCmd.Parameters.Add(new SqlParameter()
        {
            ParameterName = "@IsUnique",
            DbType = DbType.Int32,
            Direction = ParameterDirection.Output
        });


        try
        {
            sqlCnn.Open();

            sqlCmd.ExecuteNonQuery();

            sqlCnn.Close();

            // 1 means is unique
            if ((Int32)sqlCmd.Parameters["@IsUnique"].Value == 1)
            {
                return CognexDataMan.EnumCodeKind.CODE_OK;    
            }
            else // 0 means is NOT unique
            {
                return CognexDataMan.EnumCodeKind.CODE_NOK;
            }
        }
        catch (SqlException sqlEx)
        {
            // error SQL while checking code
        }
        catch (Exception ex)
        {
            // general exception
        }
    }
}

1 个答案:

答案 0 :(得分:0)

所有代码路径都必须返回一个值,包括catch块以及if / else语句之后的值:

private CognexDataMan.EnumCodeKind CheckCode(String strCodeToCheck)
            {
                String[] splitCode = strCodeToCheck.Split(new char[] { ',' });
                String strCode = splitCode[1];

                if (strCode == String.Empty)
                {
                    // code incorrect
                    return CognexDataMan.EnumCodeKind.CODE_INCORRECT;
                }
                else
                {
                    CognexDataMan.EnumCodeKind resultToReturn;

                    SqlConnection sqlCnn = new SqlConnection(GlobalData.CnnString);
                    SqlCommand sqlCmd = new SqlCommand("sp_CheckCode", sqlCnn);
                    sqlCmd.Parameters.AddWithValue("@Code", strCode); 
                    sqlCmd.Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@IsUnique",
                        DbType = DbType.Int32,
                        Direction = ParameterDirection.Output
                    });


                    try
                    {
                        sqlCnn.Open();

                        sqlCmd.ExecuteNonQuery();

                        sqlCnn.Close();

                        // 1 means is unique
                        if ((Int32)sqlCmd.Parameters["@IsUnique"].Value == 1)
                        {
                            return CognexDataMan.EnumCodeKind.CODE_OK;    
                        }
                        else // 0 means is NOT unique
                        {
                            return CognexDataMan.EnumCodeKind.CODE_NOK;
                        }
                    }
                    catch (SqlException sqlEx)
                    {
                        // error SQL while checking code
                         return CognexDataMan.EnumCodeKind.CODE_NOK;
                    }
                    catch (Exception ex)
                    {
                        // general exception
                        return CognexDataMan.EnumCodeKind.CODE_NOK;
                    }
                }
                return CognexDataMan.EnumCodeKind.CODE_NOK;
            }