-1

So, my challenge is this:

The constructor should call the base class constructor to initialize the account’s name, number, and balance. It should also call a method in its own class, setInterestRate, which should set the InterestRate variable and validate that the rate is a positive number. If the interest rate passed in is negative, set the interest rate to zero.

This seems fairly straightforward to me, but VS is pinging me for my setInteresteRate method (not all code paths return a value). I must be missing something, but I'm not sure what. Any suggestions? Here is my code:

public SavingsAccount(string AccountName, int AccountNumber, decimal Balance, double rate) : base(AccountName, AccountNumber, Balance)
    {
        InterestRate = rate;
    }

    public double setInterestRate(double rate)
    {
        if (rate >= 0)
        {
            InterestRate = rate;
        }
        else
        {
            InterestRate = 0;
        }
    }
2
  • Maybe not the best duplicate... If you don't like this one feel free to check MSDN CS0161 or search SO - stackoverflow.com/… and add more information so it is clear why your one is different. Commented Sep 22, 2014 at 1:08
  • Alexei, thank you for the link! I'm checking this out as well and bookmarking it for future challenges! Commented Sep 22, 2014 at 1:46

1 Answer 1

1

Make your setInterestRate method void like below. because you only need to set the InterestRate.

 public void setInterestRate(double rate)

if you have a return type in your method you have to return a value. thats what your error shows.

Suggestion

Make your InterestRate to a property like below sample and do the validation there

double interestRate;
    public double InterestRate
    {
        get
        {
            return interestRate;
        }
        set
        {
            if (value >= 0)
            {
                interestRate = value;
            }
            else
            {
                interestRate = 0;
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for suggestion part (finding good duplicate could be better approach - OP is not exactly the first person to hit CS0161 error). Note that you may consider throwing exception on negative rate - it generally better to fail than use "random" values instead of input.
Thank you all for the information, this seems to have resolved the issue. I will attempt to find duplicates from MSDN in the future as well. Thanks again!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.