它告诉我,我创建的名为Calculate的方法“并不是所有的代码路径都会返回值”,这是作业,如果你想知道的话,我是一名编程一年级的学生。我只是想摆脱这个错误,如果我将它改为静态方法,它就会消失,但随后我遇到了验证错误的问题。
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();
}
}}
发布于 2011-11-16 19:30:42
如果IsValidData()返回false,则Calculate不会返回任何内容。它必须为所有代码路径返回一些内容。
发布于 2011-11-16 19:31:19
如果(!IsValidData())并且存在异常,则将返回。
发布于 2011-11-16 19:34:26
在您的异常处理之后,您需要返回一个值-显然此时您没有有效的值,所以您应该
return 0M;在方法的末尾。
https://stackoverflow.com/questions/8150918
复制相似问题