2011-11-16 43 views
0

它告訴我,我所做的方法計算「不是所有的代碼路徑都返回一個值」 這是你想知道的功課,我是一年級的編程學生。我只是想擺脫這個錯誤,如果我將其更改爲靜態方法,它會消失,但隨後我遇到了我所做驗證錯誤的問題。表單應用方法錯誤

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

} 

}

回答

2

Calculate不返回任何東西,如果IsValidData()返回false。它必須返回一些所有代碼路徑。

1

如果(!IsValidData())並且如果有異常,將返回什麼。

1

後您的異常處理,你需要返回一個值 - 顯然你沒有在這一點上一個有效的值,所以你應該

return 0M; 

在方法結束。

+0

謝謝,我無法弄清楚如何放置它。 – Wallaaa