Matt Winer
Matt Winer

Reputation: 535

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.

Once I did that it created this:

private void CheckValidation()
{

}

Again this is no issue for the Validated Event. However, in another section of my program I call the function 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'.

So how do I call the correct CheckValidation from within my code but still ensure that I can still call it from the events?

Upvotes: 0

Views: 322

Answers (3)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

If you are not using CheckValidation method as some event handler, then just remove parameters from this function. You will be able to call it like this 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:

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) != "";
}

Upvotes: 0

Trisped
Trisped

Reputation: 6001

If you callCheckValidation(); it will run the overload without parameters. If you call CheckValidation(null, EventArgs.Empty); it will run the overload with an object, EventArgs parameters.

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).

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).

Upvotes: 0

C.Evenhuis
C.Evenhuis

Reputation: 26446

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); - however a better solution is put the code in the CheckValidation() overload (without the parameters) and call that from the handler:

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

private void CheckValidation()
{
    // Code here
}

Upvotes: 4

Related Questions