简体   繁体   English

如何摆脱函数中的此错误消息?

[英]How do I get rid of this error message in my function?

I have a function in a class but am receiving an error under "Withdraw", "not all code paths return a value". 我在一个类中有一个函数,但是在“提现”下收到错误,“并非所有代码路径都返回一个值”。 I thought adding a void would do the trick but can't seem to make it go away. 我以为添加空隙可以解决问题,但似乎无法消除它。 Does know how I could revise my code? 知道如何修改代码吗? Here is the portion: 这是一部分:

public virtual double Withdraw(double amount)
  {
     if (amount > balance)
     {
        MessageBox.Show("Debit amount exceeded account balance.", "Insufficient funds!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     }
     else
        return balance - amount;
  }

Since you've declared that your function returns a double , it needs to do that whichever branch of the if gets taken. 由于您已声明函数返回double ,因此无论if采用if ,都需要执行该操作。

You need to return a value from the true side of the if , after MessageBox returns, eg.: MessageBox返回之后,您需要从iftrue面返回一个值,例如:

if (amount > balance)
{
    MessageBox.Show(...);
    return balance;
}
else ...

Not a direct answer but I think your Method is serving many purposes, the calculation and showing a message to the user, you should consider using two methods like this 这不是一个直接的答案,但我认为您的Method有许多用途,可以进行计算并向用户显示一条消息,您应该考虑使用这样的两种方法

public virtual double Withdraw(double amount)
{
    if (amount > balance)    
        throw new Exception("your message")        
    else
        return balance - amount;
}

code of the caller 呼叫者的代码

try{
 Withraw(...)
}
catch{
 Your messageBox
}

your ode should always return some value, in any condition, so 您的ode在任何情况下都应始终返回一些值,因此

public virtual double Withdraw(double amount)
  {
     if (amount > balance)
     {
        MessageBox.Show("Debit amount exceeded account balance.", "Insufficient funds!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return SOME_NON_VALID_VALUE_FOR_YOUR_APP; //or raise an exception,
        // depends on architecture 
     }

     return balance - amount;       
  }

considering the logic of the code provided, if amount > balance it's not correct , otherwise return computation. 考虑提供的代码的逻辑,如果amount > balance不正确,否则返回计算。

You line of code below does not return any value are main root cause: 您下面的代码行未返回任何值是主要的根本原因:

if (amount > balance)
 {
    MessageBox.Show("Debit amount exceeded account balance.", "Insufficient funds!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 }

You should return a double value after MessageBox.Show . 您应该在MessageBox.Show之后返回一个双MessageBox.Show值。

You need to retrn something after MessageBox.Show idealy 0 您需要在MessageBox.Show之后撤消某些操作。显示理想状态0

  public virtual double Withdraw(double amount)
  {
     if (amount > balance)
     {
        MessageBox.Show("Debit amount exceeded account balance.", "Insufficient funds!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return 0;
     }
     else
        return balance - amount;
  }

Your function is expected to returns a double but if amount>balance it will not. 您的函数应该返回一个双精度值,但是如果amount>balance则不会。

public virtual double Withdraw(double amount)
{
    if (amount > balance)
    {
        //your messagebox code    
        return double.NaN; // or whatever you think is correct in this case.
    }
    else
        return balance - amount;
}

You are returning double value through your function. 您正在通过函数返回double值。

But only mentioned in else part. 但仅在其他部分中提及。

If , if(amount > balance) condition gets true, then also you will have to return the value. 如果,if(金额>余额)条件为true,则还必须返回该值。

See below code: 请参阅以下代码:

    public virtual double Withdraw(double amount)
      {
         if (amount > balance)
         {
            MessageBox.Show("Debit amount exceeded account balance.", "Insufficient funds!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         }
         else
            return balance - amount;

         return 0;
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM