简体   繁体   English

错误的函数被调用或方法没有重载

[英]Wrong Function being called or No Overload for Method

I have a function that I use to validate the contents of some text boxes after the user browses for a directory. 在用户浏览目录后,我具有用于验证某些文本框内容的功能。

private void CheckValidation(object sender, EventArgs e)
{
    bool OK = true;
    if (PhotograherNumber.Text == string.Empty || errorProvider1.GetError(PhotograherNumber)!="")
    {
        OK = false;
    }
    if (EventNumber.Text == string.Empty || errorProvider1.GetError(EventNumber)!="")
    {
        OK = false;
    }
    if (OK)
    {
        EnableProcessNow();
    }
    else
    {
        DisableProcessNow();
    }

}

That works great. 效果很好。

But then I added the function to be called by the Validated event on the text box. 但随后我在文本框中添加了Validated事件要调用的函数。

Once I did that it created this: 一旦完成,它就创建了:

private void CheckValidation()
{

}

Again this is no issue for the Validated Event. 同样,对于Validated事件,这不是问题。 However, in another section of my program I call the function CheckValidation(); 但是,在程序的另一部分中,我调用了CheckValidation();函数CheckValidation(); . But when I do that it doesn't call the correct one. 但是,当我这样做时,它并不能称为正确的方法。

Obviously if I delete the empty 显然,如果我删除空白

private void CheckValidation()
{

}

Then I get the error 'No Overload for Method CheckValidation takes 0 Arguments'. 然后,我收到错误消息:“方法CheckValidation的任何重载都无需接受0个参数”。

So how do I call the correct CheckValidation from within my code but still ensure that I can still call it from the events? 因此,如何在代码中调用正确的CheckValidation ,但仍然确保仍可以从事件中调用它?

From what I understand of your question is you want to run the same method, but both be able to call it manually without parameters and allow it to be an event handler? 从我对您的问题的了解中,您想运行相同的方法,但是两者都能够在不带参数的情况下手动调用它并使它成为事件处理程序?

If you want to call the handler method from your code you can just call CheckValidation(null, EventArgs.Empty); 如果要从代码中调用处理程序方法,则可以只调用CheckValidation(null, EventArgs.Empty); - however a better solution is put the code in the CheckValidation() overload (without the parameters) and call that from the handler: -但是,更好的解决方案是将代码放入CheckValidation()重载(不带参数),并从处理程序中调用它:

private void CheckValidation(object sender, EventArgs e)
{
    CheckValidation();
}

private void CheckValidation()
{
    // Code here
}

If you are not using CheckValidation method as some event handler, then just remove parameters from this function. 如果您没有将CheckValidation方法用作某些事件处理程序,则只需从此函数中删除参数即可。 You will be able to call it like this CheckValidation(); 您将可以像这样调用CheckValidation(); . If this method used as some event handler, then also remove parameters from CheckValidation method (anyway you don't use them) and just call it inside that event handler: 如果此方法用作某些事件处理程序,则还要从CheckValidation方法中删除参数(无论如何不要使用它们),然后在该事件处理程序内调用它:

private void SomeEventHandler(object sender, EventArgs e)
{
    CheckValidation();
}

Also consider removing boolean flag from your method: 还可以考虑从方法中删除布尔值标志:

private void CheckValidation()
{
    if (PhotograherNumber.Text == "" || errorProvider1.GetError(PhotograherNumber)!= "")
    {
        DisableProcessNow();
        return;
    }

    if (EventNumber.Text == "" || errorProvider1.GetError(EventNumber)!= "")
    {
         DisableProcessNow();
         return;
    }

    EnableProcessNow();
}

And moving further, I'd create method which describes what actually you are checking: 进一步讲,我将创建描述您实际检查内容的方法:

private void CheckValidation()
{
    if (!IsAllInputValid())
    {
        DisableProcessNow();
        return;
    }

    EnableProcessNow();
}

private bool IsAllInputValid()
{
    if (!HasValidInput(PhotograherNumber))
        return false;

    if (!HasValidInput(EventNumber))
        return false;

    return true;
}

private bool HasValidInput(TextBox textBox)
{
    if (String.IsNullOrEmpty(textBox.Text)
        return false;

    return errorProvider1.GetError(textBox) != "";
}

If you call CheckValidation(); 如果您调用CheckValidation(); it will run the overload without parameters. 它会在没有参数的情况下运行重载。 If you call CheckValidation(null, EventArgs.Empty); 如果调用CheckValidation(null, EventArgs.Empty); it will run the overload with an object, EventArgs parameters. 它将使用对象EventArgs参数运行重载。

Make sure your external call is passing the correct number of parameters to do the job. 确保您的外部呼叫传递了正确数量的参数来完成这项工作。

Personally, if I am calling a method used by a form event I pass this as the sender, but it depends on the model you are using and your object security (some objects are not safe to pass around). 就个人而言,如果我调用表单事件所使用的方法,则将this作为发送者进行传递,但这取决于您使用的模型和对象的安全性(某些对象不安全地传递)。

objectReference.CheckValidation(this, EventArgs.Empty);

Also note that if you are calling the method externally it needs to be public and must be called off an instance of the class (unless the method is static where it must be called from the type name). 还要注意,如果从外部调用该方法,则该方法必须是公共的,并且必须从该类的实例中调用(除非该方法是静态的,必须在类型名称中进行调用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM