【问题标题】:C# - 3 Tier Architecture. Error says No overload for method name takes 0 argumentsC# - 3 层架构。错误说方法名称没有重载需要 0 个参数
【发布时间】:2016-04-16 06:55:49
【问题描述】:

我在我的 C# 窗口窗体中使用 3 层架构。我想做的是,如果数据存在,则隐藏按钮。这是我的代码。

类文件

public bool checkIfExists(Variables variables) { // BelPar
        SqlCommand check = new SqlCommand();
        check.Connection = dbcon.getcon();
        check.CommandType = CommandType.Text;
        check.CommandText = "SELECT * FROM tbl";
        SqlDataReader drCheck = check.ExecuteReader();
        if(drCheck.HasRows == true)
        {
            drCheck.Read();
            if (... && .. ||) // conditions where variables are being fetch
            {
                return false;
            }
        }
        drCheck.Close();
        return true;
}

窗体

btn_save.Visible = !balpayrolldetails.checkIfExists(); // This is where I get the "No overload for method 'checkIfExists' takes 0 arguments.

有什么帮助吗?请在下方留言或回答。谢谢

【问题讨论】:

  • 错误已经说明了。您需要函数 checkIfExists 的参数(变量类型)
  • 当然,checkIfExists 需要一个参数。查看您的方法定义 public bool checkIfExists(Variables variables) 您没有传递 Variables 参数
  • 为了简单支持无参数调用,可以将签名改为checkIfExists(Variables variables=null)
  • 顺便说一下和3-tier没有关系,最好把它从title中去掉,去掉tag:)

标签: c# winforms 3-tier


【解决方案1】:

要调用一个方法,你需要通过它的确切名称来调用它,在这种情况下是:

checkIfExists(Variables variables);

这告诉我们,要使用这个方法,我们需要将它传递到一个Variables 类型的对象中,以便在方法执行中使用。

必须提供方法签名中列出的任何类型才能成功调用方法。

您需要从

更新您的通话
btn_save.Visible = !balpayrolldetails.checkIfExists();

btn_save.Visible = !balpayrolldetails.checkIfExists(someVariablesOfTheExpectedType);

【讨论】:

  • @Matt-Hensly “someVariableOfTheExpectedType”是什么意思? :)
  • @mark,这就是你的变量。您将根据您使用的特定变量对其进行更改。
【解决方案2】:

拥有方法签名:

public bool checkIfExists(Variables variables)

应该通过将Variables 类型的对象传递给方法来调用它:

btn_save.Visible = !balpayrolldetails.checkIfExists(anInstanceOfVariables);

但是,如果您可以接受无参数调用方法,并且您的方法的编写方式可以容忍 variablesnull 值,您可以将签名更改为:

public bool checkIfExists(Variables variables=null)

然后你可以这样称呼它:

btn_save.Visible = !balpayrolldetails.checkIfExists();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多