从C#中的方法返回一个对象

时间:2017-09-04 15:25:05

标签: c#

我想在方法中作为计算结果返回一个对象,但我得到“无法将类型'ConsoleApp2Iteration.LoanOut'隐式转换为'double'。 “返回新的LoanOut”有下划线。出了什么问题?我需要三个输出(InterestPaymentAnnual等)作为其他地方的其他计算的输入。

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;
    }
}

4 个答案:

答案 0 :(得分:3)

您的方法被声明为返回一个double并且您有一个语句

return new LoanOut(InterestPaymentAnnual, RePymntAnnual, LoanOutstanding); 

将方法的返回类型更改为LoanOut:

public LoanOut LoanCalc() 

答案 1 :(得分:0)

您有方法public double LoanCalc(),但您尝试return new LoanOut()。因此错误是正确的,您无法将LoanOut转换为double。更改方法签名以返回LoanOut

此外,如果粘贴的代码格式是您在编辑器中的格式,我强烈建议您重新格式化代码以使其更具可读性。

答案 2 :(得分:0)

如果我正确读取此权限,您希望返回LoanOut,但是您正在尝试将您从LoanOut转换为double的错误。然而,你的方法明确表示你将返回双重身份:

public LoanOut LoanCalc()

我会尝试用LoanOut替换double:

JsonConverter

这有帮助吗?

答案 3 :(得分:0)

正如其他人所说,你试图返回一个LoanOut实例和一个double。

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

return InterestPymntP1; //Returning double

如果您需要(或想要)从方法调用获得两个值,您应该查看out个参数。 例如。

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

您必须为InterestPayment分配调用代码才能使用它。

或者,您可以更改LoanOut课程,以便在该对象中包含您的“InterestPayment”信息。如果InterestPayment信息与LoanOut实例无关,那么你就不会这样做......看起来情况可能并非如此。

另外,如果您确实修复此问题并保持问题中显示的方法结构,那么您在return new LoanOut行之后直接无法访问代码......

InterestPaymentAnnual = 0;
RePymntAnnual = 0;

永远不会运行