Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this class
public class MonthlyPayment
    {
        public int Luna { get; set; }
        public double SoldInitial { get; set; }
        public double PlataLunara { get; set; }
        public double Dobanda { get; set; }
        public double Principal { get; set; }
        public double Asigurare { get; set; }
        public double Comision { get; set; }
        public double TotalLunar { get; set; }
        public double SoldCredit { get; set; }
    }

And this data context
public class CreditDataContext
    {
        public List<MonthlyPayment> MonthlyPayments { get; set; }
        public void LoadMonthlyPayments()
        {
            MonthlyPayments = new List<MonthlyPayment>();
            MonthlyPayment payment = null;
            payment = new MonthlyPayment()
            {
                Luna = 0,
                SoldInitial = 0.00,
                PlataLunara = 0.00,
                Dobanda = 0.00,
                Principal = 0.00,
                Asigurare = 0.00,
                Comision = 200.00,
                TotalLunar = 200.00,
                SoldCredit = 10000.00f,
            };
            MonthlyPayments.Add(payment);
            for(int i=1;i<=12;i++)
            {
                MonthlyPayments.Add(InitializeMyObject(i));
                    }
        }
       public MonthlyPayment InitializeMyObject(int i)
        {
            MonthlyPayment x = new MonthlyPayment();
            x.Luna = i;
            x.SoldInitial = 10000;
            x.Dobanda = ((9.75 / 100) / 12) * x.SoldInitial;
            double v = 1 + x.Dobanda;
            x.PlataLunara = (x.SoldCredit * x.Dobanda) / (1 - Math.Pow(v, -12));
            x.Principal = x.PlataLunara - x.Dobanda;
            x.Asigurare = (0.10 / 100) * x.SoldInitial;
            x.Comision = (0.04 / 100) * x.SoldInitial;
            x.TotalLunar = x.Dobanda + x.Asigurare + x.PlataLunara;
            x.SoldCredit = x.SoldInitial - x.PlataLunara;
            return x;
        }
    }

The problem is that displays the same value for each item in the list.
How do I change the function to display values ​​calculated based on the previous item?

What I have tried:

I tried to acces de (i-1) element in the for.But does it work.
Posted
Updated 3-May-21 19:26pm

1 solution

Pass the previous MonthlyPayment item to the InitializeMyObject method:
C#
previousMonth = MonthlyPayments.Add(InitializeMyObject(i, previousMonth));

public MonthlyPayment InitializeMyObject(int i, MonthlyPayment lastMonth)
   {
   MonthlyPayment x = new MonthlyPayment();
   ... you can use lastMonth to access the previous months values.


By the way ...
C#
MonthlyPayments.Add(payment);
for(int i=1;i<=12;i++)
{
    MonthlyPayments.Add(InitializeMyObject(i));
        }
Indent your code correctly!
And since you add a payment before the loop, you end up with thirteen months in your collection ...
 
Share this answer
 
Comments
Member 15180481 4-May-21 5:06am    
I don't understarnd for what use
previousMonth = MonthlyPayments.Add(InitializeMyObject(i, previousMonth));
Can you explain?
OriginalGriff 4-May-21 5:24am    
Which bit don't you understand?
Member 15180481 4-May-21 5:36am    
Where exactly do I have to add previousMonth= MonthlyPayments.Add(InitializeMyObject(i, previousMonth));?
wherever I add it I have the error 'the name previousMonth does not exist in the current context'
OriginalGriff 4-May-21 6:19am    
Oh come on, man!
It's an example: you are supposed to learn from it and implement it yourself...
Member 15180481 4-May-21 7:04am    
In the end ,I solved it and it works.Thanks for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900