无法隐式将“void”转换为“bool”

时间:2013-06-24 00:15:54

标签: c# asp.net

有人能指出我的代码有什么问题吗?第一个函数在与第二个函数不同的aspx文件上。

    protected void btnManageUsersAddUser_Click(object sender, EventArgs e)
{
    if (clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.mdb"), txtManageUsersName.Text, txtManageUsersPassword.Text, ddlSecurityLevel.SelectedValue))
    {
        lblAddUserMsg.Text = "The user was successfully added";
        grdManagePersonnel.DataBind();
    }
    else
    {
        lblAddUserMsg.Text = "The user was not successfully added";
    }

以下函数最初是“bool”而不是“void”,但是我的教授告诉我将其更改为“void”,因为错误并非全部返回值。

    public static void SaveUser(string Database, string UserName, string UserPassword, string SecurityLevel)
{

    bool recordSaved;

    try
    {
        // Create connection
        OleDbConnection conn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;" +
                                                   "Data Source=" + Database);
        conn.Open();
        OleDbCommand command = conn.CreateCommand();
        string strSQL;

        // Insert to tblUserLogin
        strSQL = "Insert into tblUserLogin " +
                 "(UserName, UserPassword, SecurityLevel) values ('" +
                 UserName + "', '" + UserPassword + "', '" + SecurityLevel + "')";

        // Process data
        command.CommandType = CommandType.Text;
        command.CommandText = strSQL;

        // Add your comments here
        command.ExecuteNonQuery();

        // Closes the transaction when true
        conn.Close();
        recordSaved = true;


    }
            catch (Exception ex)
    {

    }

}

3 个答案:

答案 0 :(得分:5)

由于您已将方法返回类型更改为键入 void ,因此您无法再在此处的条件语句中使用它:

if (clsDataLayer.SaveUser(Server.MapPath("PayrollSystem_DB.mdb"), 
    txtManageUsersName.Text, txtManageUsersPassword.Text, ddlSecurityLevel.SelectedValue))

...条件期望表达式减少为布尔值。

您的教授可能有一个观点,即并非所有路径都返回了上一版代码中的值。如果方法返回布尔值,则需要确保所有路径都返回true或false值。例如,您可以修改代码以再次返回布尔值并返回以下值:

...
return true;

}
catch (Exception ex)
{
   return false;
}
...

请注意,我删除了“recordSaved”变量,因为它是不必要的;如果你只想在那个地方使用它,我建议你自己返回真/假值。

答案 1 :(得分:2)

是的,你把它改成什么都没有,但是你仍然期望它会返回一些东西,因为你还在尝试使用结果:

if (clsDataLayer.SaveUser( ...

要么改变期望(失去将有价值的信息返回给调用者的能力),要么返回原始版本并确保所有代码路径都返回一个值。

你教授的建议类似于:

  

你:我的车轮胎漏气了   教授:好吧,把轮胎关掉   你:呃,现在我的车仍然不会去。

虽然教授建议拆除瘪胎确实解决了当前问题(因为你的车不再有瘪胎),但这并不是一个合适的解决方案。在不理解问题的根本原因(a) 的情况下改变事物经常导致您目前发现自己的情况。

你的教授应该建议你理解为什么你得到错误并修复那个,而不是选择快速修复其他地方的分支。


(a)这个问题的根本原因并不是你的值返回一个布尔值,而是因为调用者期望的内容和被调用者提供的内容之间存在不匹配。

答案 2 :(得分:1)

我不同意你的教授的建议。将方法的返回类型更改为void,因为所有路径都不返回值,就像在受感染的剪切上打一个绷带并期望它愈合一样。

更好的解决方案IMO将确保所有路径执行返回一个值(true或false)。

例如,在您的方法中,更改:

bool recordSaved;

为:

bool recordSaved = false;

然后,如果在try部分的末尾(catch行之前),请添加:

recordSaved = true;

然后在退出方法之前返回recordSaved

return recordSaved;

通过这些更改,您的方法会将recordSaved值设置为false;如果保存记录,它只会被设置为true。然后,您可以返回该值,并使用if检查中的方法。

完整的代码看起来像这样:

public static bool SaveUser(string Database, string UserName, string UserPassword, string SecurityLevel)
{

    bool recordSaved = false;

    try
    {
        // do your save
        recordSaved = true;
    }
    catch (Exception ex)
    {
       // Handle the exception (logging, etc)
    }

    return recordSaved;
}
相关问题