Assuming that "ItemTypeName", "ItemCategoryName" and so forth are all text boxs or similar, refactor it to a method:
private bool CheckValidity(TextBox tb, ErrorConstant fail)
{
if (tb.Text == string.Empty || tb.Text == StringConstants.select ||
tb.SelectedValue.ToString() == StringConstants.select)
{
this.Master.LblError.Text= fail;
this.Master.LblError.ForeColor = Color.Red;
tb.Focus();
return false;
}
return true;
}
Then just call it repeatedly:
return CheckValidity(ItemTypeName,ErrorConstants.selectItemType) &&
CheckValidity(ItemCategoryName,ErrorConstants.selectItemCat) &&
CheckValidity(ManufacturerName,ErrorConstants.selectMake) &&
CheckValidity(PackagingByName,ErrorConstants.selectPackage);
"But here, I've Four different textboxes.
Then how can I take them in one 'If Statement'?
actually i'm having 4 combo boxes not textboxes"
TextBox/ComboBox - it makes no difference! Just change the parameter type.
When you do a statement such as
bool b = bool1 && bool2 && bool3;
it evaluates them in strict order: Bool1 first. If this is true, evaluate bool2. If it isn't true, go no further because the whole expression is false.
As soon as the compiler gets to the point where a single
false
is encountered, it stops processing.
So:
return CheckValidity(ItemTypeName,ErrorConstants.selectItemType) &&
CheckValidity(ItemCategoryName,ErrorConstants.selectItemCat) &&
CheckValidity(ManufacturerName,ErrorConstants.selectMake) &&
CheckValidity(PackagingByName,ErrorConstants.selectPackage);
will return
true
if and only if all of your TextBoxes / ComboBoxes pass the validation. If any one of them fails, it will stop processing, and return
false
;