Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.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# 去掉try块中的条件语句_C#_Wpf_Try Catch - Fatal编程技术网

C# 去掉try块中的条件语句

C# 去掉try块中的条件语句,c#,wpf,try-catch,C#,Wpf,Try Catch,我正在用c#构建我的第一个WPF应用程序。下面是包含try-catch块的部分代码。但是,它有代码重复。在try块中不使用if-else语句就可以拥有相同的功能吗。有人能提出更好的方法吗?也许我们可以抛出算术异常,但我对所有这些东西都不熟悉,使用异常(1/0)和(1%0)会直接产生错误 目的:检查文本框3是否包含10位数字。如果是,则在texbox4中显示该数字以及其他一些数据。如果否,则显示错误消息 private void Button_Click_1(object sender,

我正在用c#构建我的第一个WPF应用程序。下面是包含try-catch块的部分代码。但是,它有代码重复。在try块中不使用if-else语句就可以拥有相同的功能吗。有人能提出更好的方法吗?也许我们可以抛出算术异常,但我对所有这些东西都不熟悉,使用异常(1/0)和(1%0)会直接产生错误

目的:检查文本框3是否包含10位数字。如果是,则在texbox4中显示该数字以及其他一些数据。如果否,则显示错误消息

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {

            textbox3.Text = (Convert.ToInt64(textbox3.Text)).ToString();
            if ((textbox3.Text).Length == 10)
            {
                textbox4.Text = textbox1.Text + Environment.NewLine + textbox2.Text + Environment.NewLine + textbox3.Text; 
            }

            else 
            {
                textbox3.Text = string.Empty;
                textbox4.Text = string.Empty;
                MessageBox.Show("Please, enter a 10 digit Contact No.", "Error");
            }
        }

        catch
        {
            textbox3.Text = string.Empty;
            textbox4.Text = string.Empty;
            MessageBox.Show("Please, enter a 10 digit Contact No.", "Error");
        }
    }

因为似乎只有
Convert.ToInt64
才会抛出异常,所以我可能会这样做

bool error = false;
try
{       
    textbox3.Text = (Convert.ToInt64(textbox3.Text)).ToString();
}
catch
{ error = true; }

if ((textbox3.Text).Length == 10)
{
    textbox4.Text = textbox1.Text + Environment.NewLine + textbox2.Text + Environment.NewLine + textbox3.Text; 
}
else
    error = true;

if (error)
{
    textbox3.Text = string.Empty;
    textbox4.Text = string.Empty;
    MessageBox.Show("Please, enter a 10 digit Contact No.", "Error");
}

如果您的目标是测试
textbox3.Text
是否包含10位数字,则可以使用正则表达式


TryParse
然后使用结果。那么就不需要
try/catch
。关于可重复代码,有两个局部变量
newTextBox3
newTextBox4
,默认值为
“”
。如果没有错误,请更改它们。如果出现错误-显示
消息框
。始终在函数末尾将其值设置为
textbox3
textbox4
(将其视为缓冲输出)。为什么不使用验证类?如果用户输入9位数字,但将负值作为代码,该怎么办?例如-123456789;似乎这个输入通过了测试“Convert.ToInt64(textbox3.Text)).ToString().Length=10”我的实际目的是检查它是否是10位数字。
  if (Regex.IsMatch(textbox3.Text, @"^\d{10}$")) 
    textbox4.Text = textbox1.Text + Environment.NewLine + 
                    textbox2.Text + Environment.NewLine + 
                    textbox3.Text; 
  else {
    textbox3.Text = string.Empty;
    textbox4.Text = string.Empty;
    MessageBox.Show("Please, enter a 10 digit Contact No.", "Error");
  }