Aurelius
Aurelius

Reputation: 27

Returning an object from a method in C#

I want to return an object as the result of a calculation in a method but I get "Cannot implicitly convert type 'ConsoleApp2Iteration.LoanOut' to 'double". The "return new LoanOut" is underlined. What is going wrong? I need the three outputs (InterestPaymentAnnual etc.) as input to other calculations elsewhere.

public class Loan
{  
 > initialisers here  
    public double LoanCalc()
    {
        double nper = LoanYrs * NprPrYr;//payment terms per yr  
 `double EffectiveRate = Math.Pow(1 + (InterestPct + ContribPct),  
(Math.Pow(NprPrYr, -1))) - 1;`   
//interest per payment term
        double Interest_Contribution = InterestPct + ContribPct;
        double length = nper;
        double InterestPaymentP1 = 0;
        {
            Pymnt = Financial.Pmt(EffectiveRate, nper, LoanNPV, 0, 0);
            LoanOutstanding = LoanNPV;           
        for (int i = 1; i <= length; i++)
             {
// code block removed for clarity   
            if (i % NprPrYr != 0)
// testing for full years as derived calculations use input inyears                    
                    {
                    InterestPaymentAnnual += Interest_ContributionUSD;
                    RePymntAnnual += RePymnt;
                    }
            else
                   {
                    InterestPaymentAnnual += Interest_ContributionUSD;
                    RePymntAnnual += RePymnt;
 // new object which containts annual interest payment, annual repayment and    
 //   remaining loan amount
                   return new LoanOut(InterestPaymentAnnual, RePymntAnnual,    
                    LoanOutstanding);
 //initialisation before new payment cycle
                    InterestPaymentAnnual = 0;
                    RePymntAnnual = 0;
                }
            }
        }
        return InterestPymntP1;
    }
}

public class LoanOut
{
    public double InterestPaymentAnnual { get; private set; }
    public double RePymntAnnual { get; private set; }
    public double LoanOutstanding { get; private set; }
    double InterestPymntP1 = 0;
    double Repayment = 0;
    double DebtRemaining = 0;
     public LoanOut(double InterestPaymentAnnual, double RePymntAnnual, 
     double LoanOutstanding)
    {
        this.InterestPaymentAnnual = InterestPymntP1;
        this.RePymntAnnual = Repayment;
        this.LoanOutstanding = DebtRemaining;
    }
}

Upvotes: 1

Views: 4813

Answers (4)

Skintkingle
Skintkingle

Reputation: 1579

As others have stated, you are trying to return both a LoanOut instance, and a double.

return new LoanOut(InterestPaymentAnnual, RePymntAnnual, LoanOutstanding); //Returning LoanOut Instance

and

return InterestPymntP1; //Returning double

if you need (or want) to gain two values from a method call, you should look at out parameters. EG.

public LoanOut LoanCalc(out double InterestPayment)
{
    //Your stuff here
}

You have to assign to InterestPayment for the calling code to use it.

Alternatively you can change your LoanOut class to also contain your 'InterestPayment' informaiton on that object. You would only NOT do that if the InterestPayment info doesn't relate to the LoanOut instance... which looking at it probably isn't the case.

Also, if you do fix this and keep the method structured as displayed in your question, You have unreachable code directly after the return new LoanOut line...

InterestPaymentAnnual = 0;
RePymntAnnual = 0;

will never get run

Upvotes: 0

That&#39;sIs JustCrazy
That&#39;sIs JustCrazy

Reputation: 143

If i'm reading this right, you want to return a LoanOut but you're getting the error that you are trying to convert from LoanOut to double. Your method, however specifically says that you'll be returning a double:

public double LoanCalc()

I would try replacing double with LoanOut like this:

public LoanOut LoanCalc()

Does that help?

Upvotes: 0

Katulus
Katulus

Reputation: 751

You have method public double LoanCalc() but you try to return new LoanOut(). So the error is correct, you can't convert LoanOut to double. Change the method signature to return LoanOut.

Also, if the pasted code formatting is what you have in your editor I would strongly suggest to reformat your code to make it more readable.

Upvotes: 0

Kell
Kell

Reputation: 3317

Your method is declared as returning a double and you have a statement

return new LoanOut(InterestPaymentAnnual, RePymntAnnual, LoanOutstanding); 

Change the return type of your method to LoanOut:

public LoanOut LoanCalc() 

Upvotes: 3

Related Questions