0

I am creating a project which will consist of three forms, one parent form which will be a decider on which form to load. Due to the two child forms being slightly similar, a lot of validation is the same. I have my methods for validation which are then called in the Validating event how can I minimise the amount of duplication in the Validating events and just have the one method with one validating event which has contol over all of my controls that share the method.

this is one example of my methods thats used extensively throughout:

public bool numValidation(string strNum)       
{
    if (!string.IsNullOrWhiteSpace(strNum))
    {
        int temp;
        if (int.TryParse(strNum, out temp))
        {
            MessageBox.Show("Phone Number is a valid input: " + temp);
            return true;
        }
        else
        {
            MessageBox.Show(temp + "Is not Valid input!!");                            
        }        
    }

    return false;
}

And this is just one of many of my Validating events :

private void txt_LndLine_Validating(object sender, CancelEventArgs e)
{
    numValidation(txt_LndLine.Text);
    txt_LndLine.Clear();
}

Just really wondering thoughts on how I could clean up the code and minimise duplication. Thanks in advance :)

  • 1
    Create a library project and store this common methods in a static utility class. And don't put UI elements and logic in this library. Just return true/false and let the UI part show the messages – Steve Jun 02 '17 at 08:07
  • Thanks @Steve So just create the static utill class and put all of my methods that validate in there. Then just call them by creating an instance of that class and call the method? or should i inhereit ? – whatdoyouNeedFromMe Jun 02 '17 at 08:17
  • 1
    More or less. First you don't put everything there but just the common code that can be reused without being logically tied to some kind of classes. Second you don't need to create instances if your class or methods are static. (IE for a class named Utility with a static method named numValidation you call _Utility.numValidation(.....)_ – Steve Jun 02 '17 at 08:19
  • And if i have to not include any Ui elements, how will i minimise the amount of Validating events, which are related to the TextBox? – whatdoyouNeedFromMe Jun 02 '17 at 08:20
  • Cheers will update once i have coded the utill class – whatdoyouNeedFromMe Jun 02 '17 at 08:23
  • 1
    The methods are static exactly because you don't tie them to any specific business logic of a particular class and are all in a class named Utility. You don't minimize the Validation events. They belong to the UI layer of your forms or classes. You just avoid to put a MessageBox in the utility class where it is not appropriate for reusability (what if you want to fail silently sometime? OK you can add a parameter but this becomes quickly a mess). Instead take the resul of the validation to the UI layer where you put the UI Message. – Steve Jun 02 '17 at 08:26

2 Answers2

2

If you have a lot of common stuff, Declarative Validation could help you to clean your code.

MSDN - Extending Windows Forms with a Custom Validation Component Library

You could also handle multiple control validating events with the same Sub, using an helper class to reuse a validation calculation function.

Last but not least, have a read about IDataErrorInfo. With this interface, you could put all validation algo in your business class.

It is working fine in Winforms with the ErrorProvider component and databinding

Marco Guignard
  • 613
  • 3
  • 9
0

Thanks to @Steve solved and better code. Was solved by creating a seperate static class and simply calling the method in all of my Validating Events in both pages that had the common functions.

  private void txt_Fax_Validating(object sender, CancelEventArgs e)
        {
            Utillity.numValidation(txt_Fax.Text);
            txt_Fax.Clear();

        }

And the Utillity Class was as follows:

public static bool numValidation(string strNum)
    {
        if (!string.IsNullOrWhiteSpace(strNum))
        {
            int temp;
            if (int.TryParse(strNum, out temp))
            {
                Console.WriteLine("Phone Number is a valid input: " + temp);
                return true;
            }
            else
            { Console.WriteLine(temp + "Is not Valid input!!"); }
        }
        return false;
    }