The plot is this - I have a Save() method which calls a validation method withing itself and I want to be sure that if the validation method caught an error, the execution of the Save() method will be stopped. What I've done is making a bool method for the validation:
protected virtual bool IsNullOrEmptyControl(params Control[] controls) { bool validationFlag = false; foreach (Control ctrl in controls) { if (string.IsNullOrWhiteSpace(ctrl.Text)) { ctrl.BackColor = System.Drawing.Color.Yellow; if (validationFlag == false) { ctrl.Focus(); validationFlag = true; } } } if (validationFlag == true) { MessageBox.Show("The fields in yellow could not be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } return true; } And calling it from withing my Save() method:
public bool Save() { some code... IsNullOrEmptyControl(txtClientCode, txtClientName); some code.. clientService.Save(entity); } I thought that because my IsNullOrEmptyControl() method is bool if it returns false then this will mean stop of further code execution in Save() but it seems I was wrong. So what's the right way to do this?