C# 如何编写简单的ErrorMessage函数

C# 如何编写简单的ErrorMessage函数,c#,asp.net,error-handling,try-catch,C#,Asp.net,Error Handling,Try Catch,我试图理解,如何编写简单的错误消息函数,如果在文本框中输入字符串而不是数字,该函数会做出反应 假设我想计算值1和值2,但若输入了字符串,则在标签中显示错误 示例 1+1=2 a+1=错误 我的代码 计算.cs public static string ErrorMessage() { string msg = ""; try { //do sth } catch (Exception

我试图理解,如何编写简单的错误消息函数,如果在文本框中输入字符串而不是数字,该函数会做出反应

假设我想计算值1和值2,但若输入了字符串,则在标签中显示错误

示例

1+1=2

a+1=错误

我的代码

计算.cs

public static string ErrorMessage()
    {
        string msg = "";
        try
        {
            //do sth
        }
        catch (Exception ex)
        {
            msg = "Wrong value";
        }
        return msg;
    }
public static bool ErrorMessage(string value1, string value2)
    {
        bool check = true;
        string error;
        if (value1 != "" && value2 != "")
        {
            check = true;
        }
        if (value1 =="" || value2 =="")
        {
            check = false;
            error = "Error!";
        }
        return check;    
    }
计算器。asxc

protected void Button1_Click(object sender, EventArgs e)
    {
    try
        {

            //calculate - works
        }
    catch
        {
             Error.Text = Calculate.ErrorMsg();
        }
protected void Button1_Click(object sender, EventArgs e)
    {
    try
        {

            //calculate - works
        }
        //
        catch
        {
        bool res = false;

            res = Calculate.ErrorMessage(textBox1.Text, textBox2.Text);

            Error.Text = res.ToString();
        }
也尝试过类似的方法,但似乎不起作用:

计算.cs

public static string ErrorMessage()
    {
        string msg = "";
        try
        {
            //do sth
        }
        catch (Exception ex)
        {
            msg = "Wrong value";
        }
        return msg;
    }
public static bool ErrorMessage(string value1, string value2)
    {
        bool check = true;
        string error;
        if (value1 != "" && value2 != "")
        {
            check = true;
        }
        if (value1 =="" || value2 =="")
        {
            check = false;
            error = "Error!";
        }
        return check;    
    }
计算器。asxc

protected void Button1_Click(object sender, EventArgs e)
    {
    try
        {

            //calculate - works
        }
    catch
        {
             Error.Text = Calculate.ErrorMsg();
        }
protected void Button1_Click(object sender, EventArgs e)
    {
    try
        {

            //calculate - works
        }
        //
        catch
        {
        bool res = false;

            res = Calculate.ErrorMessage(textBox1.Text, textBox2.Text);

            Error.Text = res.ToString();
        }
我知道第二种方法不检查数字,但我只是尝试实现一些逻辑,看看ti是否有效,但什么都不起作用


我迷路了…请帮助

据我所知,如果用户输入的是字符串而不是数字,您需要您的应用程序显示错误消息

您应该使用
Int32.Parse()
Int32.TryParse()
方法。 更多关于和的信息,请点击此处

方法TryParse足够好,因为如果无法将字符串解析为整数,它不会抛出错误,而是返回false

这里是如何在类中使用此方法的示例,更改按钮1\u单击方法如下:

protected void Button1_Click(object sender, EventArgs e)
{
    int a;
    int b;

    // Here we check if values are ok
    if(Int32.TryParse(textBox1.Text, out a) && Int32.TryParse(textBox2.Text, b))
    {
        // Calculate works with A and B variables
        // don't know whats here as you written (//calculate - works) only
    }
    // If the values of textBoxes are wrong display error message
    else
    {
        Error.Text = "Error parsing value! Wrong values!";
    }
}
如果您需要使用ErrorMessage方法,那么这里是如何更改ErrorMessage方法的,但这更复杂,第一个示例更简单:

public static string ErrorMessage(string value1, string value2)
{
    int a;
    int b;

    // If we have an error parsing (note the '!')
    if(!Int32.TryParse(value1, out a) || !Int32.TryParse(value2, b))
    {
        return "Error parsing value! Wrong values!";
    }

    // If everything is ok
    return null;
}

希望这有帮助,询问您是否需要更多信息。

是的,您正处于我要做的事情的关键时刻。。。但我该如何在另一个网络表单中显示void函数呢?也。。。假设函数ErrorMessage()检查获取设置值value1、value2…编辑第一个示例并添加ErrorMessage示例(如果需要此函数)。看看,这很好用!我将尝试通读代码并理解它。谢谢你,伙计!