C# 窗体应用 - 停止执行驻留在帮助程序类中的帮助程序方法中的所有代码



我刚刚学会了如何将参数传递到方法中,所以我正在重构我的代码以使其更干净。我创建了一个新的"ValidateInput"类,其中包含一个ValidateFinancialsInput方法,我将字符串传递给该方法。然后它检查字符串以查看它是否正确,如果不是,我想显示一个 messageBox,然后停止执行所有代码。如果我使用"return;",它只是恢复执行 Parent 方法。如何停止执行 ValidateFinancialsInput 方法中的所有代码?我尝试研究一段时间无济于事。这是我的代码:

Class Parent
{    
   private void button2_Click(object sender, EventArgs e)
    {     var CompanyVar = comboBox1.Text;
            ValidateInput vi = new ValidateInput();
            vi.ValidateFinancialsInput(CompanyVar);
             //the rest of my code for the application is here
             //the rest ...
              //the rest...
      }
}
class ValidateInput
{
    public void ValidateFinancialsInput(string Co)
    {
        string[] validCompany = { "BVV", "LWDO" };
        if (validCompany.Contains(Co) == false)
        {
            MessageBox.Show("You have entered an invalid company.");
           //what do I put here to stop all code execution?
        }

    }
}

您应该尝试使用返回值状态意图来调用方法

Class Parent
{    
 private void button2_Click(object sender, EventArgs e)
 {     var CompanyVar = comboBox1.Text;
        ValidateInput vi = new ValidateInput();
        if(!vi.ValidateFinancialsInput(CompanyVar))
        {
            MessageBox.Show("You have entered an invalid company.");
            return;
        }
         //the rest of my code for the application is here
         //the rest ...
          //the rest...
  }
}
class ValidateInput
{
 public bool ValidateFinancialsInput(string Co)
 {
    string[] validCompany = { "BVV", "LWDO" };
    if (validCompany.Contains(Co) == false)
    {
       return false;
    }
    return true;
 }
}

我在这里所做的是返回一个 true|false 值来指示验证是否已通过,如果尚未通过,则显示 MessageBox,否则它继续执行"其他"代码。

希望这有帮助

最简单的方法是有一个例外:

class Parent
{
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            var CompanyVar = comboBox1.Text;
            ValidateInput vi = new ValidateInput();
            vi.ValidateFinancialsInput(CompanyVar);
            //the rest of my code for the application is here
            //the rest ...
            //the rest...
        }
        catch (ValidationException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}
class ValidationException : Exception
{
    public ValidationException(string message) : base(message)
    {
    }
}
class ValidateInput
{
    public void ValidateFinancialsInput(string Co)
    {
        string[] validCompany = { "BVV", "LWDO" };
        if (validCompany.Contains(Co) == false)
        {
            throw new ValidationException("You have entered an invalid company.");
        }
    }
}

这将停止ValidateFinancialsInput的执行,并在catch (ValidationException ex)内移动执行button2_Click您可以在其中决定如何处理验证错误

你有一个类,它的全部目的是验证,所以你可以添加一个公共方法IsValidated

您可以使用该类添加更多内容,例如,列出它违反的所有业务规则,并通过其他方法或属性返回它们。

class ValidateInput
{
  public bool IsValidated {get; private set}
  public bool ValidateFinancialsInput(string Co)
  {
     string[] validCompany = { "BVV", "LWDO" };
     this.IsValidated = validCompany.Contains(Co)
  }
}

此类应该只知道验证过程,而不应对任何其他操作。

你有几个选择。看起来您的程序中有按钮,所以我猜这不是控制台应用程序。 如果您希望应用程序完全停止,您可以使用Application.Exit或查看Environment.Exit https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110(.aspx

但是,我建议使用例外,这样您就不会终止整个程序:

 try
        {
            var CompanyVar = comboBox1.Text;
            ValidateInput vi = new ValidateInput();
            vi.ValidateFinancialsInput(CompanyVar);
            //the rest of my code for the application is here
            //the rest ...
            //the rest...
        }
        catch (ValidationException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
 public void ValidateFinancialsInput(string Co)
    {
        string[] validCompany = { "BVV", "LWDO" };
        if (validCompany.Contains(Co) == false)
        {
            throw new ValidationException("You have entered an invalid company.");
        }
    }

最新更新