-10

I have a problem with getting this right. All i get is unreachable code detected. I can run the program with that warning but it doesn't work correctly. What is it that i don't see?

this is the last part of my code:

class Account
    {        
       private double balance = 0;


       public Account()
       {
       }//end construcor

        //Constructor initializes balance with an amount supplied as argument
       public Account(double amount)
        {
            Balance = amount;
        }// end constructor    

       //property to get and set the balance value
        public double Balance
        {
            get
            {
                return balance;
            }
            set
            {
                balance = value;
            }
        }// end property


        public void ShowMessage(string str)
        {
            Console.WriteLine(str);
        }


        public double Deposit(double depositAmount)
        {

            Balance += depositAmount * 1.05;
            return Balance ;
        }

        public double Withdraw(double WithdrawAmount)
        {
            Balance -= WithdrawAmount;
            return Balance;

            if (WithdrawAmount > this.Balance)
                this.ShowMessage("You do not have enough money!");
            else
                this.Balance -= WithdrawAmount;
            return this.Balance;
        }
    }
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • 3
    What do you expect your `Withdraw` method to do? You have a `return` in the middle of it. Do you understand what the `return` keyword does? – dee-see Oct 07 '14 at 21:30
  • It is the Withdraw part that is the problem, wrong use of if and else statements, but I do not know how to do that. – JackDaniels Oct 07 '14 at 21:31
  • 1
    No; you simply need to understand what `return` means and when to use it. This has nothing to do with `if`. – SLaks Oct 07 '14 at 21:36
  • The withdraw metod should tell me that if I withdraw more than what i have, it displays the error message "You do not have enough money!" and if i withdraw without exceeding the amount that i have in the account then it should tell me what I have left. Was is a little bit blurr or did u understand? :) – JackDaniels Oct 07 '14 at 21:36
  • 2
    I'm pretty sure we understand fine. We are trying to say the problem does not lie with your if statement, but with your early return. – BradleyDotNET Oct 07 '14 at 21:37

3 Answers3

3

The entire last block of Withdraw is unreachable because you return in the second line, which returns the argument to the caller and exits the function.

It looks like you should just remove the first two lines entirely.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

Your 2nd line return Balance; will exit the method there, and so the if/else is never reached.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
1

This is the problem:

public double Withdraw(double WithdrawAmount)
    {
        Balance -= WithdrawAmount;
        return Balance;

        if (WithdrawAmount > this.Balance)
            this.ShowMessage("You do not have enough money!");
        else
            this.Balance -= WithdrawAmount;
        return this.Balance;
    }

The Second line ends the life of the function.

  Balance -= WithdrawAmount;  
  return Balance;

that's my solution:

The Withdraw logic as I see it: 

 if (Balance < WithdrawAmount)
 {
    ShowMessage("You do not have enough money!");
 }
 else
 {
    Balance -= WithdrawAmount;
 }

 return Balance;

If you can't Withdraw, print a message else you withdraw the amount.

at any case, you return the current Balance.

Tal.Bary
  • 450
  • 2
  • 16