In a Win Form I'm doing initial validation in the Form. So before saving data, I want to validate whether all the required fields are filled (Text Boxes) by the user. There are about 18 such Text Boxes in the Form. Currently I'm doing it as follows. To make the code short only three fields are shown in the code.
private void cmbSave_Click(object sender, EventArgs e)
{
if(IsFilled (txtApplicationNumber.Text))
{
if(IsFilled (txtEmployeeID.Text))
{
if (IsFilled(txtNIC.Text))
Save(sender, e);
}
}
}
private bool IsFilled(string s)
{
if (s != "")
{ return true; }
else
{ return false; }
}
Another option would be to use the logic inside the cmbSave_Click straightaway as follows,
private void cmbSave_Click(object sender, EventArgs e)
{
If (txtApplicationNumber.Text!="" & txtEmployeeID.Text!="" & txtNIC.Text="")
Save(sender, e)
}
Basically the code should be readable, consistent and should perform the validation properly.
Which option could be considered better? If both are not good please propose a way to do this?
string.IsNullOrEmpty()
orstring.IsNullOrWhiteSpace()
. \$\endgroup\$