首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单应用方法错误

表单应用方法错误
EN

Stack Overflow用户
提问于 2011-11-16 19:27:38
回答 3查看 365关注 0票数 0

它告诉我,我创建的名为Calculate的方法“并不是所有的代码路径都会返回值”,这是作业,如果你想知道的话,我是一名编程一年级的学生。我只是想摆脱这个错误,如果我将它改为静态方法,它就会消失,但随后我遇到了验证错误的问题。

代码语言:javascript
复制
    private decimal Calculate(decimal operand1, decimal operand2, string operator1)
    {
        //Declarations
        decimal operandOne;
        decimal operandTwo;
        string operatorOne = "";
        const string divide = "/";
        const string multiply = "*";
        const string addition = "+";
        decimal calcResult = 0m;

        try
        {
            if (IsValidData())
            {

                // try to get the user input from the form
                operandOne = Convert.ToDecimal(txtOperandOne.Text);
                operandTwo = Convert.ToDecimal(txtOperandTwo.Text);
                operatorOne = Convert.ToString(txtOperator.Text);

                if (operatorOne == divide)
                {
                    calcResult = operandOne / operandTwo;

                }
                else if (operatorOne == multiply)
                {
                    calcResult = operandOne * operandTwo;

                }
                else if (operatorOne == addition)
                {
                    calcResult = operandOne + operandTwo;

                }
                else
                {
                    calcResult = operandOne - operandTwo;

                }
                return (Math.Round(calcResult, 4));

            }

        }
        catch (FormatException myFormatEx)
        {
            MessageBox.Show(myFormatEx.Message + "\nInvalid numeric format. Please check all entries.", "Entry Error");
        }
        catch (OverflowException myOverflowEx)
        {
            MessageBox.Show(myOverflowEx.Message + "\nOverflow error. Please enter smaller values.", "Entry Error");
        }
        catch (Exception myEx)
        {
            MessageBox.Show(myEx.Message + "\n\n" + myEx.GetType().ToString() + "\n" + myEx.StackTrace, "Exception");
        }


    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        //DECLARATIONS
        decimal calcResult;
        decimal operandOne = 0m;
        decimal operandTwo = 0m;
        string operatorOne = "";

        //PROCESSING
        calcResult = Calculate(operandOne, operandTwo, operatorOne);

        //OUTPUT
        lblResult.Text = calcResult.ToString("d"); // in decimal format


    }
    private bool IsValidData()
    {
        // This method checks all the textboxes on the form for valid entries


        return
            // Validate the OperandOne text box
            IsPresent(txtOperandOne, "First Operator") &&
            IsDecimal(txtOperandOne, "First Operator") &&


            // Validate the OperandTwo text box
            IsPresent(txtOperandTwo, "Second Operator") &&
            IsDecimal(txtOperandTwo, "Second Operator") &&


            // Validate the Operator text box
            IsPresent(txtOperator, "Operator /,*,+ or -") &&
            IsOperator(txtOperator, "Operator /,*,+ or -");
    }
    public bool IsOperator(TextBox textBox, string name)
    {
        // this method makes sure a textbox is a valid operator
        string validOperators = "";
        bool valid = true;
        try
        {
            validOperators = Convert.ToString(textBox.Text); // try to convert




            if (validOperators != "/" | validOperators != "*" | validOperators != "+" | validOperators != "-") // not valid entry
            {
                MessageBox.Show(name + " must be a valid operator +,-,/,* Entry Error");
                textBox.SelectAll();
                valid = false;
            }
        }
        catch (FormatException myFormatEx)
        {
            textBox.SelectAll(); // Select the user's entry
            throw myFormatEx; // throw to the calling method to handle
        }
        catch (OverflowException myOverflowEx)
        {
            throw myOverflowEx; // throw to the calling method to handle
        }
        catch (Exception myEx)
        {
            throw myEx; // throw to the calling method to handle
        }
        return valid;

    }
    public bool IsPresent(TextBox textBox, string name)
    {
        // this method checks any textbox for a required entry
        bool valid = true; // assuming valid 
        if (textBox.Text == "") // check to see if there is an entry
        {
            MessageBox.Show(name + " is a required field.", "Entry Error");
            textBox.Focus(); // set the focus
            valid = false;
        }
        return valid;
    }

    public bool IsDecimal(TextBox textBox, string name)
    {
        // this method checks any textbox for a valid decimal entry
        bool valid = true; // assuming valid 

        try
        {
            Convert.ToDecimal(textBox.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show(name + " must be a decimal value.", "Entry Error");
            textBox.SelectAll(); // Select the user's entry
            valid = false;
        }
        catch (OverflowException myOverflowEx)
        {
            throw myOverflowEx; // throw to the calling method to handle
        }
        catch (Exception myEx)
        {
            throw myEx; // throw to the calling method to handle
        }

        return valid;
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        //End program
        this.Close();
    }

}

}

EN

回答 3

Stack Overflow用户

发布于 2011-11-16 19:30:42

如果IsValidData()返回false,则Calculate不会返回任何内容。它必须为所有代码路径返回一些内容。

票数 2
EN

Stack Overflow用户

发布于 2011-11-16 19:31:19

如果(!IsValidData())并且存在异常,则将返回。

票数 1
EN

Stack Overflow用户

发布于 2011-11-16 19:34:26

在您的异常处理之后,您需要返回一个值-显然此时您没有有效的值,所以您应该

代码语言:javascript
复制
return 0M;

在方法的末尾。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8150918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档