1
\$\begingroup\$

I have this:

public void InfoAssignment()
{
    //MessageBox.Show(firstName = firstNameBox.Text);
    firstName = firstNameBox.Text;
    lastName = lastNameBox.Text;
    phoneNumber = phoneBox.Text;
    address = addressBox.Text;

    //Check for text in firstNameBox
    if (!String.IsNullOrWhiteSpace(firstName))
    {
        firstName = firstNameBox.Text;
    }
    else
    {
        MessageBox.Show("Please enter a first name");
        return;
    }

    if (!String.IsNullOrWhiteSpace(lastName))
    {
        lastName = lastNameBox.Text;
    }
    else
    {
        MessageBox.Show("Please enter a last name");
        return;
    }

    if(!String.IsNullOrWhiteSpace(phoneNumber))
    {
        phoneNumber = phoneBox.Text;
    }
    else
    {
        MessageBox.Show("Please enter a phone number");
        return;
    }

    if (!String.IsNullOrWhiteSpace(address))
    {
        address = addressBox.Text;
    }
    else
    {
        MessageBox.Show("Please enter a phone number");
        return;
    }
    //firstName = firstNameBox.Text;
    //lastName = lastNameBox.Text;
}

It does what I need it to do but obviously this is basically repeating the same thing with only minor variations. I'm thinking there has to be a better way of which I'm unaware. Is there?

\$\endgroup\$
1
  • \$\begingroup\$ You can do your checks directly on the textbox values. \$\endgroup\$
    – Arghya C
    Dec 3 '15 at 16:15
1
\$\begingroup\$

Here you go.

Pass the variables by reference and let the method update them if the input is valid.

Do processing if all the inputs are valid.

EDIT: I needed to change the method name from ValidateInput to CopyToVariableWithValidation because the former fails to have a good name to represent it's function.

private bool CopyToVariableWithValidation(TextBox textBox, string inputFriendlyName, ref string variableToCopyContent)
{
    if (string.IsNullOrWhiteSpace(textBox.Text))
    {
        MessageBox.Show("Please enter a " + inputFriendlyName);
        return false;
    }
    variableToCopyContent = textBox.Text;
    return true;
}

public void InfoAssignment()
{
    if
    (
        CopyToVariableWithValidation(firstNameBox, "First Name", ref firstName) &&
        CopyToVariableWithValidation(lastNameBox, "Last Name", ref lastName) &&
        CopyToVariableWithValidation(phoneBox, "Phone Number", ref phoneNumber) &&
        CopyToVariableWithValidation(addressBox, "Address", ref address)
    )
    {
        // Input is valid. Do more processing here.
    }
}
\$\endgroup\$
4
\$\begingroup\$

You could create a method for it!

bool Validate(string val, TextBox textBox, string description)
{
    if (!string.IsNullOrWhiteSpace(val))
    {
        val = textBox.Text;
    }
    else
    {
        MessageBox.Show("Please enter " + description");
        return false;
    }

    return true;
}

Then you could use it like so:

if (!Validate(firstName, firstNameBox, "a first name"))
   return;
if (!Validate(lastName, lastNameBox, "a last name"))
   return;
\$\endgroup\$
0
\$\begingroup\$

You have a copy-and-paste error: it does MessageBox.Show("Please enter a phone number"); when the address is missing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.