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.
You need to return a value from the true
side of the if
, after MessageBox
returns, eg.:
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
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
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.
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
.
You need to retrn something after MessageBox.Show
idealy 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.
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.
But only mentioned in else part.
If , if(amount > balance) condition gets true, then also you will have to return the value.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.