Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Form App-停止执行位于helper类中的helper方法中的所有代码_C# - Fatal编程技术网

C#Form App-停止执行位于helper类中的helper方法中的所有代码

C#Form App-停止执行位于helper类中的helper方法中的所有代码,c#,C#,我刚刚学会了如何将参数传递给方法,所以我正在重构我的代码以使其更干净。我已经创建了一个新的“ValidateInput”类,它包含一个ValidateFinancialsInput方法,我将字符串传递给该方法。然后检查字符串是否正确,如果不正确,我想显示messageBox,然后停止执行所有代码。如果我使用“return;”,它只会恢复父方法的执行。如何停止ValidateFinancialsInput方法中所有代码的执行?我试着研究了一会儿,但没有结果。这是我的密码: Class Parent

我刚刚学会了如何将参数传递给方法,所以我正在重构我的代码以使其更干净。我已经创建了一个新的“ValidateInput”类,它包含一个ValidateFinancialsInput方法,我将字符串传递给该方法。然后检查字符串是否正确,如果不正确,我想显示messageBox,然后停止执行所有代码。如果我使用“return;”,它只会恢复父方法的执行。如何停止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)
    {
        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
,并在
按钮2中单击
将执行移动到
捕获(ValidationException ex)
中,您可以在其中决定如何处理验证错误

最简单的方法是使用异常:

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
,并在
按钮2中单击
将执行移动到
捕获(ValidationException ex)
中,您可以决定如何处理验证错误您有几个选项。看起来你的程序中有按钮,所以我猜这不是一个控制台应用程序。如果希望应用程序完全停止,可以使用
application.Exit
或签出
Environment.Exit

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

 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.");
        }
    }

你有几个选择。看起来你的程序中有按钮,所以我猜这不是一个控制台应用程序。如果希望应用程序完全停止,可以使用
application.Exit
或签出
Environment.Exit

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

 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.");
        }
    }

您应该尝试使用返回值来表示调用方法的意图

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,否则它将继续执行“other”代码


希望这有帮助

您应该尝试使用返回值来声明调用方法的意图

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,否则它将继续执行“other”代码


希望这有帮助

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

您可以在类中添加更多内容,例如,创建一个它违反的所有业务规则的列表,并通过另一个方法或属性返回它们

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

这个类应该只知道验证过程,不应该做其他事情。

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

您可以在类中添加更多内容,例如,创建一个它违反的所有业务规则的列表,并通过另一个方法或属性返回它们

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


这个类应该只知道验证过程,不应该做其他事情。

如果它是一个
命令行
程序,只需使用
Console.readLine()在那里使用MessageBox将通过单元测试使代码不稳定。验证是业务逻辑,而不是UI。在UI中做UI工作。@ThomasWeller好东西。感谢you@ThomasWeller通过在UI中执行UI内容,您是说执行主按钮中的MessageBox 2u单击函数是否正确?@JaAustin:是的。如果它是一个
命令行
程序,只需使用
Console.readLine()在那里使用MessageBox将通过单元测试使代码不稳定。验证是业务逻辑,而不是UI。在UI中做UI工作。@ThomasWeller好东西。感谢you@ThomasWeller通过在UI中使用UI内容,你是说在主按钮中使用MessageBox 2\u单击功能是否正确?@JaAustin:是的。这有帮助,但并不能真正使代码更干净。这似乎有点违背了创建一个新类和方法来处理验证的目的。我很抱歉,但我不同意,这使代码更干净,它在一个类中保留可视代码,而在另一个类中保留验证(不是可视问题)。这大大有助于分离现在可以独立改变的两个关注点。你应该看看坚实的原则()谢谢你的文章。您的代码是否在solid中验证了S(单一责任原则)?如果主函数1)调用其他函数,2)检查它们的输出,那就是两件事。我想得对吗?也许我应该有另一个函数,其唯一目的是检查其他函数的输出这是一个常见的误解,S在solid中并不意味着“一个函数只能做一件事”,它应该在另一种意义上考虑,“一个方法或类应该只有一个改变的原因”,所以在你的例子中,如果我想更改此屏幕UI的逻辑,我必须知道ValidateInput执行基于UI的操作,并且可能需要更改。例如,我有另一个屏幕需要相同的验证,但不需要消息框,我该怎么办?这个人自己(Bob Martin)谈论他的原则更多的代码并不一定是坏事,首先你应该让你的代码可以理解。你应该努力实现模块化的、可重用的代码,这就是我的示例试图展示的,很抱歉你对我的方法不满意,也许你可以发布一个你最终得到的代码的要点,有人可以看看。你得到了什么?从我的例子来看,你的意思是什么?我刚才回答了您最初的问题,即如何停止程序中方法的执行,以及如何使用返回值来控制流,以便实现