表单应用方法错误

本文关键字:错误 方法 应用 表单 | 更新日期: 2023-09-27 18:14:42

它告诉我,我所做的方法命名为计算"不是所有的代码路径返回值"如果你想知道,这是我的家庭作业,我是一年级的编程学生。我只是想摆脱这个错误,如果我把它改为静态方法,它就会消失,但随后我遇到了我所做的验证错误的问题。

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

}

表单应用方法错误

如果IsValidData()返回false, Calculate不返回任何内容。它必须返回所有代码路径的值。

If (!IsValidData()),如果有异常,将返回什么

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

return 0M;