【问题标题】:Form application method error表单申请方法错误
【发布时间】:2011-11-16 11:27:37
【问题描述】:

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

    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();
    }

}

}

【问题讨论】:

    标签: c# calculator


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果 (!IsValidData()) 并且如果有异常,将返回什么。

      【讨论】:

        【解决方案3】:

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

        return 0M;
        

        在方法结束时。

        【讨论】:

        • 谢谢,我不知道如何放置它。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多