Hans Rudel
Hans Rudel

Reputation: 3611

User verification code, in a method/s

Evening guys,

This may seem like a stupid question but im having some issues figuring out where i should be placing my user input verification checks. I have checks, for the following:

  1. file exists
  2. Correct extension
  3. Access to the file
  4. Input in 2 NumericUpDown controls
  5. One numericUpDown is always greater than the other.
  6. Assignment to static properties.

Im assuming each one of these should at least be a single method but i then have a single method which has a large number of lines which solely check the result of the method calls to the list above. ie

public void VerifyData()
{
    if(VerifyNumber1OnTheList != true)
    {
        LogError("The file specified is incorrect")
        return;
    }

    if(VerifyNumber2OnTheList != ......

Any suggestions on how i should actually be laying this out would be appreciated.

Thanks

Upvotes: 0

Views: 148

Answers (3)

Matt
Matt

Reputation: 1431

All right here is a quick sample I wrote up for you. Architecture obviously is your choice here. Modify as needed. I believe this should be a great jumping off point for you.

Cheers

Matt

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            //This would simulate the event handler that calls your validation event

            List<string> errorList = Validation.VerifyData();


            if (errorList.Count != 0)
            {
                ErrorHandler.HandleError(errorList);
                return;
            }

            //Do stuff if validation actually passed here.

        }
    }

    public static class Validation
    {

        public static List<string> VerifyData()
        {
            List<string> errorList = new List<string>();

            //File exists
            if (true)
                errorList.Add("File doesn't exist.");

            //File has correct extension
            if (true)
                errorList.Add("File doesn't exist.");

            //Has access to the file
            if (true)
                errorList.Add("File doesn't exist.");

            //INput in 2 NumericUpDownControls
            if (true)
                errorList.Add("File doesn't exist.");

            //One NumericUpDown is always greater than the other
            if (true)
                errorList.Add("File doesn't exist.");

            //Assignment to static properties
            if (true)
                errorList.Add("File doesn't exist.");


            return errorList;
        }
    }

    public static class ErrorHandler
    {

        public static void HandleError(List<string> errorMessageList )
        {
            //Display your message here.  This could return a dialog result as well for further processing.
        }

    }



}

Upvotes: 1

pfries
pfries

Reputation: 1758

Putting a series of validation methods is ok.

This is user input, so you're doing defensive programming here, not looking for exceptional errors. It seems like you'd want to know why the user input is bad. If so, a simple approach is to use a collecting object to visit each validation method

public void VerifyData(ValidationErrors errors)
{
  ValidateFileExists(errors);
  ValidateExtension(errors);
  ValidateFileAccess(errors);
  ...
}

private void ValidateFileExists(ValidationErrors errors)
{
 if(!File.Exists...)
{
  errors.Add("File does not exists.");
}
}

public void CallingMethod(UserInput input)
{
  _dataToVerify = input;
  var errors = new ValidationErrors();
  VerifyData(errors);
  if(errors.Count > 0)
   ShowErrors(errors);
  else
   ShowSuccess();
}

I just put the data in an instance variable for purposes of simplicity, you could pass that into your validation method. This approach is simple but allows you to tell the user something about the input and how to correct it.

Upvotes: 1

Khan
Khan

Reputation: 18162

Your method above seems like a perfectly fine way of laying out your code.

My only change would be in how you handle your checking. Use well-named methods rather than booleans during your check:

public void VerifyData()
{
    if(FileExists())
    {
        LogError("The file specified is incorrect")
        return;
    }

    if(CorrectExtension())

Upvotes: 0

Related Questions