15,172,951 members
See more:
C#
```MonthlyPayments = new List<MonthlyPayment>();
MonthlyPayment payment = new MonthlyPayment()
{
Luna = 0,
SoldInitial = 0,
PlataLunara = 0,
DobandaLunara = 0,
Principal = 0,
AsigurareLunara = 0,
ComisionLunar = ComisionAnaliza,
TotalLunar = ComisionAnaliza,
SoldCredit = ValoareCredit,
};
for (int i = 2; i <= n; i++)
{

}

paymentViewSource = new CollectionViewSource();
this.DataContext = paymentViewSource;

}

public MonthlyPayment InitializeMyObject(int i, MonthlyPayment lastMonth)
{

MonthlyPayment x = new MonthlyPayment();
x.Luna = i;
x.SoldInitial = Math.Round(lastMonth.SoldCredit, 2);
double c = Comision / 12;
double d = Dobanda / 12;
x.DobandaLunara = Math.Round(d * lastMonth.SoldCredit, 2);
double v = 1 + d;
x.PlataLunara = Math.Round((ValoareCredit * d) / (1 - Math.Pow((v), -12)), 2);
x.Principal = Math.Round(x.PlataLunara - x.DobandaLunara, 2);
x.AsigurareLunara = Math.Round(Asigurare * lastMonth.SoldCredit, 2);
x.ComisionLunar = Math.Round(c * ValoareCredit, 2);
x.TotalLunar = Math.Round(x.ComisionLunar + x.AsigurareLunara + x.PlataLunara, 2);
x.SoldCredit = Math.Round(lastMonth.SoldCredit - x.Principal, 2);

return x;

}```

What do I need to add in the for(i=1;i<=n;i++) to calculate the following n rates?

What I have tried:

C#
```for (int i = 2; i <= n; i++)
{

}```

But I have error 'Cannot convert from 'object'to CreditSimulation.MonthlyPayment'.
Posted
Updated 16-May-21 11:14am

## Solution 1

You have the cast "item" of Items, which is an object collection.

`...PaymentsDataGrid.Items[i-1] as MonthlyPayment.`

Everything is an object; but not every object can pass for a MonthlyPayment.